diff --git a/cmd/orbit/orbit.go b/cmd/orbit/orbit.go index 75c1f9ae2f..b85665435c 100644 --- a/cmd/orbit/orbit.go +++ b/cmd/orbit/orbit.go @@ -309,76 +309,3 @@ func main() { log.Error().Err(err).Msg("") } } - -var shellCommand = &cli.Command{ - Name: "shell", - Aliases: []string{"osqueryi"}, - Usage: "Run the osqueryi shell", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "osquery-channel", - Usage: "Channel of osquery version to use", - Value: "stable", - EnvVars: []string{"ORBIT_OSQUERY_CHANNEL"}, - }, - &cli.BoolFlag{ - Name: "debug", - Usage: "Enable debug logging", - EnvVars: []string{"ORBIT_DEBUG"}, - }, - }, - Action: func(c *cli.Context) error { - if c.Bool("debug") { - zerolog.SetGlobalLevel(zerolog.DebugLevel) - } - - if err := os.MkdirAll(c.String("root-dir"), constant.DefaultDirMode); err != nil { - return errors.Wrap(err, "initialize root dir") - } - - localStore, err := filestore.New(filepath.Join(c.String("root-dir"), "tuf-metadata.json")) - if err != nil { - log.Fatal().Err(err).Msg("failed to create local metadata store") - } - - // Initialize updater and get expected version - opt := update.DefaultOptions - opt.RootDirectory = c.String("root-dir") - opt.ServerURL = c.String("tuf-url") - opt.LocalStore = localStore - opt.InsecureTransport = c.Bool("insecure") - updater, err := update.New(opt) - if err != nil { - return err - } - if err := updater.UpdateMetadata(); err != nil { - log.Info().Err(err).Msg("failed to update metadata. using saved metadata.") - } - osquerydPath, err := updater.Get("osqueryd", c.String("osquery-channel")) - if err != nil { - return err - } - - var g run.Group - - // Create an osquery runner with the provided options - r, _ := osquery.NewRunner( - osquerydPath, - osquery.WithShell(), - // Handle additional args after -- - osquery.WithFlags(c.Args().Slice()), - ) - g.Add(r.Execute, r.Interrupt) - - // Install a signal handler - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - g.Add(run.SignalHandler(ctx, os.Interrupt, os.Kill)) - - if err := g.Run(); err != nil { - log.Error().Err(err).Msg("unexpected exit") - } - - return nil - }, -} diff --git a/cmd/orbit/shell.go b/cmd/orbit/shell.go new file mode 100644 index 0000000000..d89d5861b3 --- /dev/null +++ b/cmd/orbit/shell.go @@ -0,0 +1,90 @@ +package main + +import ( + "context" + "os" + "path/filepath" + + "github.com/fleetdm/orbit/pkg/constant" + "github.com/fleetdm/orbit/pkg/osquery" + "github.com/fleetdm/orbit/pkg/update" + "github.com/fleetdm/orbit/pkg/update/filestore" + "github.com/oklog/run" + "github.com/pkg/errors" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + "github.com/urfave/cli/v2" +) + +var shellCommand = &cli.Command{ + Name: "shell", + Aliases: []string{"osqueryi"}, + Usage: "Run the osqueryi shell", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "osqueryd-channel", + Usage: "Channel of osqueryd version to use", + Value: "stable", + EnvVars: []string{"ORBIT_OSQUERYD_CHANNEL"}, + }, + &cli.BoolFlag{ + Name: "debug", + Usage: "Enable debug logging", + EnvVars: []string{"ORBIT_DEBUG"}, + }, + }, + Action: func(c *cli.Context) error { + if c.Bool("debug") { + zerolog.SetGlobalLevel(zerolog.DebugLevel) + } + + if err := os.MkdirAll(c.String("root-dir"), constant.DefaultDirMode); err != nil { + return errors.Wrap(err, "initialize root dir") + } + + localStore, err := filestore.New(filepath.Join(c.String("root-dir"), "tuf-metadata.json")) + if err != nil { + log.Fatal().Err(err).Msg("failed to create local metadata store") + } + + // Initialize updater and get expected version + opt := update.DefaultOptions + opt.RootDirectory = c.String("root-dir") + opt.ServerURL = c.String("tuf-url") + opt.LocalStore = localStore + opt.InsecureTransport = c.Bool("insecure") + updater, err := update.New(opt) + if err != nil { + return err + } + if err := updater.UpdateMetadata(); err != nil { + log.Info().Err(err).Msg("failed to update metadata. using saved metadata.") + } + osquerydPath, err := updater.Get("osqueryd", c.String("osquery-channel")) + if err != nil { + return err + } + + var g run.Group + + // Create an osquery runner with the provided options + r, _ := osquery.NewRunner( + osquerydPath, + osquery.WithShell(), + // Handle additional args after -- + osquery.WithFlags(c.Args().Slice()), + ) + g.Add(r.Execute, r.Interrupt) + + // Install a signal handler + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + g.Add(run.SignalHandler(ctx, os.Interrupt, os.Kill)) + + if err := g.Run(); err != nil { + log.Error().Err(err).Msg("unexpected exit") + } + + return nil + }, +}