Skip to content

Commit

Permalink
feat: Add save config feature within the config subcommand
Browse files Browse the repository at this point in the history
Summary:
Calnex builds a desired config by applying changes to the original config fetched from the device. If anything is updated, it will print the diff.

This feature allows users to show the entire config and not only the diff, even if there are no changes to the config. Which might be useful for debugging.

It is done by adding a --save <PATH> flag to Calnex's config subcommand. When --save <PATH> is provided, Calnex will dump the config file to <PATH>. Note that --save and --apply is mutually exclusive so either none is provided or one of the two.

Reviewed By: leoleovich

Differential Revision: D52538805
  • Loading branch information
aphostrophy authored and facebook-github-bot committed Jan 5, 2024
1 parent 184fad0 commit 20aff09
Show file tree
Hide file tree
Showing 4 changed files with 587 additions and 12 deletions.
1 change: 1 addition & 0 deletions calnex/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
insecureTLS bool
source string
target string
saveConfig string
)

// Execute is the main entry point for CLI interface
Expand Down
12 changes: 10 additions & 2 deletions calnex/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ func init() {
configCmd.Flags().BoolVar(&insecureTLS, "insecureTLS", false, "Ignore TLS certificate errors")
configCmd.Flags().StringVar(&target, "target", "", "device to configure")
configCmd.Flags().StringVar(&source, "file", "", "configuration file")
configCmd.Flags().StringVar(&saveConfig, "save", "", "save configuration to the specified path")
if err := configCmd.MarkFlagRequired("target"); err != nil {
log.Fatal(err)
}
if err := configCmd.MarkFlagRequired("file"); err != nil {
log.Fatal(err)
}
configCmd.MarkFlagsMutuallyExclusive("apply", "save")

Check failure on line 42 in calnex/cmd/config.go

View workflow job for this annotation

GitHub Actions / test

configCmd.MarkFlagsMutuallyExclusive undefined (type *cobra.Command has no field or method MarkFlagsMutuallyExclusive)

Check failure on line 42 in calnex/cmd/config.go

View workflow job for this annotation

GitHub Actions / lint

configCmd.MarkFlagsMutuallyExclusive undefined (type *cobra.Command has no field or method MarkFlagsMutuallyExclusive) (typecheck)
}

var configCmd = &cobra.Command{
Expand Down Expand Up @@ -65,8 +67,14 @@ var configCmd = &cobra.Command{
log.Fatalf("Failed to find config for %s in %s", target, source)
}

if err := config.Config(target, insecureTLS, dc, apply); err != nil {
log.Fatal(err)
if saveConfig != "" {
if err := config.Save(target, insecureTLS, dc, saveConfig); err != nil {
log.Fatal(err)
}
} else {
if err := config.Config(target, insecureTLS, dc, apply); err != nil {
log.Fatal(err)
}
}
},
}
50 changes: 40 additions & 10 deletions calnex/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,12 @@ func Config(target string, insecureTLS bool, cc *CalnexConfig, apply bool) error
var c config
api := api.NewAPI(target, insecureTLS)

f, err := api.FetchSettings()
f, err := prepare(&c, api, target, cc)

if err != nil {
return err
}

m := f.Section("measure")
g := f.Section("gnss")

// set base config
c.baseConfig(m, g, target, cc.AntennaDelayNS)

// set measure config
c.measureConfig(m, cc.Measure)

if !apply {
log.Info("dry run. Exiting")
return nil
Expand Down Expand Up @@ -246,3 +238,41 @@ func Config(target string, insecureTLS bool, cc *CalnexConfig, apply bool) error

return nil
}

// Save saves the Network/Calnex configs to file
func Save(target string, insecureTLS bool, cc *CalnexConfig, saveConfig string) error {
var c config
api := api.NewAPI(target, insecureTLS)

f, err := prepare(&c, api, target, cc)

if err != nil {
return err
}

err = f.SaveTo(saveConfig)

if err != nil {
return err
}

return nil
}

func prepare(c *config, api *api.API, target string, cc *CalnexConfig) (*ini.File, error) {
f, err := api.FetchSettings()
if err != nil {
return nil, err
}

m := f.Section("measure")
g := f.Section("gnss")

// set base config
c.baseConfig(m, g, target, cc.AntennaDelayNS)

// set measure config
c.measureConfig(m, cc.Measure)

return f, nil
}
Loading

0 comments on commit 20aff09

Please sign in to comment.