package hack import ( "context" "flag" "fmt" "os" "github.com/peterbourgon/ff/v3" "github.com/peterbourgon/ff/v3/ffcli" "edge-infra.dev/pkg/lib/build/bazel" ) // Hack is the root CLI container, it is embedded by all subcommands and embeds // common configuration and helpers. type Hack struct { Paths Paths } type Paths struct { RepoRoot string WorkingDir string } // New creates new `hack` command, returning the ffcli Command to execute as well // as the Hack struct created, allowing shared state to be passed to `hack` // subcommands. func New() (*ffcli.Command, *Hack) { hack := &Hack{} fs := flag.NewFlagSet("hack", flag.ExitOnError) return &ffcli.Command{ Name: "hack", Exec: hack.Exec, FlagSet: fs, Options: []ff.Option{ ff.WithEnvVarNoPrefix(), }, }, hack } func (h *Hack) Exec(_ context.Context, _ []string) error { flag.Usage() return nil } // AfterParse is ran after ffcli.Parse is called with an instance of Hack. func (h *Hack) AfterParse() error { cwd, err := bazel.ResolveWd() if err != nil { return fmt.Errorf("failed to resolve current working directory: %w", err) } if bazel.IsBazelRun() { if err := os.Chdir(cwd); err != nil { return err } } reporoot, err := bazel.FindRepoRoot(cwd) if err != nil { return fmt.Errorf("failed to find repository root: %w", err) } h.Paths = Paths{RepoRoot: reporoot, WorkingDir: cwd} return nil }