latest
This commit is contained in:
161
internal/cli/info.go
Normal file
161
internal/cli/info.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.eeqj.de/sneak/secret/internal/vault"
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/fatih/color"
|
||||
"github.com/spf13/afero"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// Version info - these are set at build time
|
||||
var ( //nolint:gochecknoglobals // Set at build time
|
||||
Version = "dev" //nolint:gochecknoglobals // Set at build time
|
||||
GitCommit = "unknown" //nolint:gochecknoglobals // Set at build time
|
||||
)
|
||||
|
||||
// InfoOutput represents the system information for JSON output
|
||||
type InfoOutput struct {
|
||||
Version string `json:"version"`
|
||||
GitCommit string `json:"gitCommit"`
|
||||
Author string `json:"author"`
|
||||
License string `json:"license"`
|
||||
GoVersion string `json:"goVersion"`
|
||||
DataDirectory string `json:"dataDirectory"`
|
||||
CurrentVault string `json:"currentVault"`
|
||||
NumVaults int `json:"numVaults"`
|
||||
NumSecrets int `json:"numSecrets"`
|
||||
TotalSize int64 `json:"totalSizeBytes"`
|
||||
OldestSecret time.Time `json:"oldestSecret,omitempty"`
|
||||
LatestSecret time.Time `json:"latestSecret,omitempty"`
|
||||
}
|
||||
|
||||
// newInfoCmd returns the info command
|
||||
func newInfoCmd() *cobra.Command {
|
||||
cli := NewCLIInstance()
|
||||
|
||||
var jsonOutput bool
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "info",
|
||||
Short: "Display system information",
|
||||
Long: "Display information about the secret system including version, vault statistics, and storage usage",
|
||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||
return cli.Info(cmd, jsonOutput)
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVar(&jsonOutput, "json", false, "Output in JSON format")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
// Info displays system information
|
||||
func (cli *Instance) Info(cmd *cobra.Command, jsonOutput bool) error {
|
||||
info := InfoOutput{
|
||||
Version: Version,
|
||||
GitCommit: GitCommit,
|
||||
Author: "Jeffrey Paul <sneak@sneak.berlin>",
|
||||
License: "WTFPL",
|
||||
GoVersion: runtime.Version(),
|
||||
DataDirectory: cli.stateDir,
|
||||
}
|
||||
|
||||
// Get current vault
|
||||
currentVault, err := vault.GetCurrentVault(cli.fs, cli.stateDir)
|
||||
if err == nil {
|
||||
info.CurrentVault = currentVault.Name
|
||||
}
|
||||
|
||||
// Count vaults
|
||||
vaultsDir := filepath.Join(cli.stateDir, "vaults.d")
|
||||
vaultEntries, err := afero.ReadDir(cli.fs, vaultsDir)
|
||||
if err == nil {
|
||||
for _, entry := range vaultEntries {
|
||||
if entry.IsDir() {
|
||||
info.NumVaults++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Gather statistics from all vaults
|
||||
if info.NumVaults > 0 {
|
||||
totalSecrets, totalSize, oldestTime, latestTime, _ := gatherVaultStats(cli.fs, vaultsDir)
|
||||
info.NumSecrets = totalSecrets
|
||||
info.TotalSize = totalSize
|
||||
if !oldestTime.IsZero() {
|
||||
info.OldestSecret = oldestTime
|
||||
}
|
||||
if !latestTime.IsZero() {
|
||||
info.LatestSecret = latestTime
|
||||
}
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
encoder := json.NewEncoder(cmd.OutOrStdout())
|
||||
encoder.SetIndent("", " ")
|
||||
|
||||
return encoder.Encode(info)
|
||||
}
|
||||
|
||||
// Pretty print with colors and emoji
|
||||
return prettyPrintInfo(cmd.OutOrStdout(), info)
|
||||
}
|
||||
|
||||
// prettyPrintInfo formats and prints the info in a pretty format
|
||||
func prettyPrintInfo(w io.Writer, info InfoOutput) error {
|
||||
const separatorLength = 40
|
||||
|
||||
bold := color.New(color.Bold)
|
||||
green := color.New(color.FgGreen)
|
||||
cyan := color.New(color.FgCyan)
|
||||
yellow := color.New(color.FgYellow)
|
||||
magenta := color.New(color.FgMagenta)
|
||||
|
||||
_, _ = fmt.Fprintln(w)
|
||||
_, _ = bold.Fprintln(w, "🔐 Secret System Information")
|
||||
_, _ = fmt.Fprintln(w, strings.Repeat("─", separatorLength))
|
||||
|
||||
_, _ = fmt.Fprintf(w, "📦 Version: %s\n", green.Sprint(info.Version))
|
||||
_, _ = fmt.Fprintf(w, "🔧 Git Commit: %s\n", cyan.Sprint(info.GitCommit))
|
||||
_, _ = fmt.Fprintf(w, "👤 Author: %s\n", cyan.Sprint(info.Author))
|
||||
_, _ = fmt.Fprintf(w, "📜 License: %s\n", cyan.Sprint(info.License))
|
||||
_, _ = fmt.Fprintf(w, "🐹 Go Version: %s\n", cyan.Sprint(info.GoVersion))
|
||||
_, _ = fmt.Fprintf(w, "📁 Data Directory: %s\n", yellow.Sprint(info.DataDirectory))
|
||||
|
||||
if info.CurrentVault != "" {
|
||||
_, _ = fmt.Fprintf(w, "🗄️ Current Vault: %s\n", magenta.Sprint(info.CurrentVault))
|
||||
} else {
|
||||
_, _ = fmt.Fprintf(w, "🗄️ Current Vault: %s\n", color.RedString("(none)"))
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprintln(w, strings.Repeat("─", separatorLength))
|
||||
|
||||
_, _ = fmt.Fprintf(w, "🗂️ Vaults: %s\n", bold.Sprint(info.NumVaults))
|
||||
_, _ = fmt.Fprintf(w, "🔑 Secrets: %s\n", bold.Sprint(info.NumSecrets))
|
||||
if info.TotalSize >= 0 {
|
||||
//nolint:gosec // TotalSize is always >= 0
|
||||
_, _ = fmt.Fprintf(w, "💾 Total Size: %s\n", bold.Sprint(humanize.Bytes(uint64(info.TotalSize))))
|
||||
} else {
|
||||
_, _ = fmt.Fprintf(w, "💾 Total Size: %s\n", bold.Sprint("0 B"))
|
||||
}
|
||||
|
||||
if !info.OldestSecret.IsZero() {
|
||||
_, _ = fmt.Fprintf(w, "🕰️ Oldest Secret: %s\n", info.OldestSecret.Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
if !info.LatestSecret.IsZero() {
|
||||
_, _ = fmt.Fprintf(w, "✨ Latest Secret: %s\n", info.LatestSecret.Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprintln(w)
|
||||
|
||||
return nil
|
||||
}
|
||||
83
internal/cli/info_helper.go
Normal file
83
internal/cli/info_helper.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
// gatherVaultStats collects statistics from all vaults
|
||||
func gatherVaultStats(
|
||||
fs afero.Fs,
|
||||
vaultsDir string,
|
||||
) (totalSecrets int, totalSize int64, oldestTime, latestTime time.Time, err error) {
|
||||
vaultEntries, err := afero.ReadDir(fs, vaultsDir)
|
||||
if err != nil {
|
||||
return 0, 0, time.Time{}, time.Time{}, err
|
||||
}
|
||||
|
||||
for _, vaultEntry := range vaultEntries {
|
||||
if !vaultEntry.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
vaultPath := filepath.Join(vaultsDir, vaultEntry.Name())
|
||||
secretsPath := filepath.Join(vaultPath, "secrets.d")
|
||||
|
||||
// Count secrets in this vault
|
||||
secretEntries, err := afero.ReadDir(fs, secretsPath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, secretEntry := range secretEntries {
|
||||
if !secretEntry.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
totalSecrets++
|
||||
secretPath := filepath.Join(secretsPath, secretEntry.Name())
|
||||
|
||||
// Get size and timestamps from all versions
|
||||
versionsPath := filepath.Join(secretPath, "versions")
|
||||
versionEntries, err := afero.ReadDir(fs, versionsPath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, versionEntry := range versionEntries {
|
||||
if !versionEntry.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
versionPath := filepath.Join(versionsPath, versionEntry.Name())
|
||||
|
||||
// Add size of encrypted data
|
||||
dataPath := filepath.Join(versionPath, "data.age")
|
||||
if stat, err := fs.Stat(dataPath); err == nil {
|
||||
totalSize += stat.Size()
|
||||
}
|
||||
|
||||
// Add size of metadata
|
||||
metaPath := filepath.Join(versionPath, "metadata.age")
|
||||
if stat, err := fs.Stat(metaPath); err == nil {
|
||||
totalSize += stat.Size()
|
||||
}
|
||||
|
||||
// Track timestamps
|
||||
if stat, err := fs.Stat(versionPath); err == nil {
|
||||
modTime := stat.ModTime()
|
||||
if oldestTime.IsZero() || modTime.Before(oldestTime) {
|
||||
oldestTime = modTime
|
||||
}
|
||||
if latestTime.IsZero() || modTime.After(latestTime) {
|
||||
latestTime = modTime
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return totalSecrets, totalSize, oldestTime, latestTime, nil
|
||||
}
|
||||
@@ -41,6 +41,7 @@ func newRootCmd() *cobra.Command {
|
||||
cmd.AddCommand(newEncryptCmd())
|
||||
cmd.AddCommand(newDecryptCmd())
|
||||
cmd.AddCommand(newVersionCmd())
|
||||
cmd.AddCommand(newInfoCmd())
|
||||
|
||||
secret.Debug("newRootCmd completed")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user