2026-05-16 09:55:54 +05:30
|
|
|
package config
|
2026-05-04 21:38:29 +05:30
|
|
|
|
|
|
|
|
import (
|
2026-05-16 09:55:54 +05:30
|
|
|
"encoding/json"
|
2026-05-04 21:38:29 +05:30
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
|
2026-05-16 09:55:54 +05:30
|
|
|
appConfig "github.com/safedep/pmg/config"
|
2026-07-18 22:53:39 +05:30
|
|
|
"github.com/safedep/pmg/internal/editor"
|
2026-05-04 21:38:29 +05:30
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-16 09:55:54 +05:30
|
|
|
func NewConfigCommand() *cobra.Command {
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
|
Use: "config",
|
|
|
|
|
Short: "View and modify PMG configuration",
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
return cmd.Help()
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd.AddCommand(newGetCommand())
|
|
|
|
|
cmd.AddCommand(newSetCommand())
|
|
|
|
|
cmd.AddCommand(newEditCommand())
|
|
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newGetCommand() *cobra.Command {
|
|
|
|
|
return &cobra.Command{
|
|
|
|
|
Use: "get <key>",
|
|
|
|
|
Short: "Get a config value by dot-notation key (output is JSON)",
|
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
|
SilenceUsage: true,
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
value, err := appConfig.GetConfigValue(args[0])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := json.Marshal(value)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to marshal value: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = fmt.Fprintln(cmd.OutOrStdout(), string(data))
|
|
|
|
|
return err
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newSetCommand() *cobra.Command {
|
|
|
|
|
return &cobra.Command{
|
|
|
|
|
Use: "set <key> <value>",
|
|
|
|
|
Short: "Set a config value by dot-notation key",
|
|
|
|
|
Args: cobra.ExactArgs(2),
|
|
|
|
|
SilenceUsage: true,
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
return appConfig.SetConfigValue(args[0], args[1])
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newEditCommand() *cobra.Command {
|
2026-05-04 21:38:29 +05:30
|
|
|
return &cobra.Command{
|
|
|
|
|
Use: "edit",
|
|
|
|
|
Short: "Open the PMG config file in your default editor",
|
|
|
|
|
Long: `Open the PMG config file in your default editor.
|
|
|
|
|
|
|
|
|
|
The editor is resolved in this order:
|
|
|
|
|
1. $VISUAL
|
|
|
|
|
2. $EDITOR
|
|
|
|
|
3. Platform default (vi on Unix, notepad on Windows)
|
|
|
|
|
|
|
|
|
|
If the config file does not exist, a template is created first.`,
|
|
|
|
|
SilenceUsage: true,
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
return runEdit()
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func runEdit() error {
|
2026-05-16 09:55:54 +05:30
|
|
|
cfg := appConfig.Get()
|
2026-05-21 14:32:30 +05:30
|
|
|
if cfg.IsManaged() {
|
|
|
|
|
return appConfig.NewManagedConfigError()
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-04 21:38:29 +05:30
|
|
|
path := cfg.ConfigFilePath()
|
|
|
|
|
|
|
|
|
|
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
2026-05-16 09:55:54 +05:30
|
|
|
if err := appConfig.WriteTemplateConfig(); err != nil {
|
2026-05-04 21:38:29 +05:30
|
|
|
return fmt.Errorf("failed to create config file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to stat config file %q: %w", path, err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 22:53:39 +05:30
|
|
|
return editor.Open(path)
|
2026-05-04 21:38:29 +05:30
|
|
|
}
|