Skip to content

Commit

Permalink
(#8) Support opt-out of os.Args
Browse files Browse the repository at this point in the history
Problem:
Cannot embed machine room as a cobra subcommand because fisk and cobra
fight over os.Args

Solution:
Add a CustomArgs and CustomArgsEnabled machine room option to pass args
through.

Result:
Machine room can run as a cobra subcommand:

```go
cmd := &cobra.Command{
  Args:               cobra.ArbitraryArgs,
  DisableFlagParsing: true,
  RunE: func(cmd *cobra.Command, args []string) error {
    app, _ := mr.New(mr.Options{
      CustomArgsEnabled: true,
      CustomArgs:        args,
    })
    return app.Run(cmd.Context())
  },
}
```
  • Loading branch information
jeffmccune committed Apr 16, 2024
1 parent dbd9249 commit e112acb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ func (c *cliInstance) Application() *fisk.Application {
func (c *cliInstance) Run(ctx context.Context) error {
c.ctx, c.cancel = context.WithCancel(ctx)

c.cli.MustParseWithUsage(os.Args[1:])
args := os.Args[1:]
if c.opts.CustomArgsEnabled {
args = c.opts.CustomArgs
}
c.cli.MustParseWithUsage(args)

return nil
}
Expand Down
6 changes: 6 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func (o roOptions) NatsNeySeedFile() string { return o.opts.NatsNeyS
func (o roOptions) NatsCredentialsFile() string { return o.opts.NatsCredentialsFile }
func (o roOptions) StartTime() time.Time { return o.opts.StartTime }
func (o roOptions) ConfigBucketPrefix() string { return o.opts.ConfigBucketPrefix }
func (o roOptions) CustomArgsEnabled() bool { return o.opts.CustomArgsEnabled }
func (o roOptions) CustomArgs() []string { return o.opts.CustomArgs }

func (o *Options) roCopy() *roOptions {
return &roOptions{*o}
Expand Down Expand Up @@ -121,6 +123,10 @@ type Options struct {
// NoNetworkFacts disables built-in network interface facts gathering
NoNetworkFacts bool `json:"no_network_facts,omitempty"`

// os.Args opt-out https://github.com/choria-io/machine-room/issues/8
CustomArgsEnabled bool `json:"-"`
CustomArgs []string `json:"-"`

// Read only below...

// ConfigurationDirectory is the directory the configuration file is stored in (RO)
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (s *server) createServerNKey() error {

func (s *server) provisionConfig(f string, bi *build.Info) (*config.Config, error) {
if !choria.FileExist(bi.ProvisionJWTFile()) {
return nil, fmt.Errorf("provisioming token not found in %s", bi.ProvisionJWTFile())
return nil, fmt.Errorf("provisioning token not found in %s", bi.ProvisionJWTFile())
}

err := s.createServerNKey()
Expand Down

0 comments on commit e112acb

Please sign in to comment.