Skip to content

Commit

Permalink
move config parsing out of init because it messes up other subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
0pcom committed Feb 10, 2024
1 parent d3d83eb commit 35d114a
Showing 1 changed file with 47 additions and 52 deletions.
99 changes: 47 additions & 52 deletions cmd/dmsgpty-cli/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ var (
)

func init() {
RootCmd.PersistentFlags().StringVar(&cli.Net, "clinet", cli.Net, "network to use for dialing to dmsgpty-host")
RootCmd.PersistentFlags().StringVar(&cli.Addr, "cliaddr", cli.Addr, "address to use for dialing to dmsgpty-host")
RootCmd.PersistentFlags().StringVarP(&confPath, "confpath", confPath, defaultConfPath, "config path")
cobra.OnInitialize(initConfig)
RootCmd.Flags().StringVarP(&cli.Net, "clinet", "n", cli.Net, "network to use for dialing to dmsgpty-host")
RootCmd.Flags().StringVarP(&cli.Addr, "cliaddr", "r", cli.Addr, "address to use for dialing to dmsgpty-host")
RootCmd.Flags().StringVarP(&confPath, "confpath", "p", defaultConfPath, "config path")
RootCmd.Flags().Var(&remoteAddr, "addr", "remote dmsg address of format 'pk:port'\n If unspecified, the pty will start locally\n")
RootCmd.Flags().StringVarP(&cmdName, "cmd", "c", cmdName, "name of command to run\n")
RootCmd.Flags().StringSliceVarP(&cmdArgs, "args", "a", cmdArgs, "command arguments")
Expand All @@ -57,6 +56,50 @@ var RootCmd = &cobra.Command{
DisableSuggestions: true,
DisableFlagsInUseLine: true,
PreRun: func(*cobra.Command, []string) {
// source whitelist from config file
// by default : it will look for config
//
// case 1 : config file is new (does not contain a "wl" key)
// - create a "wl" key within the config file
//
// case 2 : config file is old (already contains "wl" key)
// - load config file into memory to manipulate whitelists
// - writes changes back to config file
println(confPath)

if _, err := os.Stat(confPath); err != nil {
cli.Log.Fatalf("Config file %s not found.", confPath)
}

// read file using ioutil
file, err := os.ReadFile(confPath) //nolint:gosec
if err != nil {
cli.Log.Fatalln("Unable to read ", confPath, err)
}

// store config.json into conf to manipulate whitelists
err = json.Unmarshal(file, &conf)
if err != nil {
cli.Log.Errorln(err)
// ignoring this error
b, err := json.MarshalIndent(conf, "", " ")
if err != nil {
cli.Log.Fatalln("Unable to marshal conf")
}

// write to config.json
err = os.WriteFile(confPath, b, 0600)
if err != nil {
cli.Log.Fatalln("Unable to write", confPath, err)
}
}
conf.CLIAddr = dmsgpty.ParseWindowsEnv(conf.CLIAddr)
if conf.CLIAddr != "" {
cli.Addr = conf.CLIAddr
}
if conf.CLINet != "" {
cli.Net = conf.CLINet
}
if remoteAddr.Port == 0 {
remoteAddr.Port = dmsgpty.DefaultPort
}
Expand Down Expand Up @@ -107,51 +150,3 @@ const help = "Usage:\r\n" +
"{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}\r\n\r\n" +
"Global Flags:\r\n" +
"{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}\r\n\r\n"

// initConfig sources whitelist from config file
// by default : it will look for config
//
// case 1 : config file is new (does not contain a "wl" key)
// - create a "wl" key within the config file
//
// case 2 : config file is old (already contains "wl" key)
// - load config file into memory to manipulate whitelists
// - writes changes back to config file
func initConfig() {

println(confPath)

if _, err := os.Stat(confPath); err != nil {
cli.Log.Fatalf("Config file %s not found.", confPath)
}

// read file using ioutil
file, err := os.ReadFile(confPath) //nolint:gosec
if err != nil {
cli.Log.Fatalln("Unable to read ", confPath, err)
}

// store config.json into conf to manipulate whitelists
err = json.Unmarshal(file, &conf)
if err != nil {
cli.Log.Errorln(err)
// ignoring this error
b, err := json.MarshalIndent(conf, "", " ")
if err != nil {
cli.Log.Fatalln("Unable to marshal conf")
}

// write to config.json
err = os.WriteFile(confPath, b, 0600)
if err != nil {
cli.Log.Fatalln("Unable to write", confPath, err)
}
}
conf.CLIAddr = dmsgpty.ParseWindowsEnv(conf.CLIAddr)
if conf.CLIAddr != "" {
cli.Addr = conf.CLIAddr
}
if conf.CLINet != "" {
cli.Net = conf.CLINet
}
}

0 comments on commit 35d114a

Please sign in to comment.