...

Source file src/edge-infra.dev/pkg/tools/hack/hack.go

Documentation: edge-infra.dev/pkg/tools/hack

     1  package hack
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  	"os"
     8  
     9  	"github.com/peterbourgon/ff/v3"
    10  	"github.com/peterbourgon/ff/v3/ffcli"
    11  
    12  	"edge-infra.dev/pkg/lib/build/bazel"
    13  )
    14  
    15  // Hack is the root CLI container, it is embedded by all subcommands and embeds
    16  // common configuration and helpers.
    17  type Hack struct {
    18  	Paths Paths
    19  }
    20  
    21  type Paths struct {
    22  	RepoRoot   string
    23  	WorkingDir string
    24  }
    25  
    26  // New creates new `hack` command, returning the ffcli Command to execute as well
    27  // as the Hack struct created, allowing shared state to be passed to `hack`
    28  // subcommands.
    29  func New() (*ffcli.Command, *Hack) {
    30  	hack := &Hack{}
    31  
    32  	fs := flag.NewFlagSet("hack", flag.ExitOnError)
    33  
    34  	return &ffcli.Command{
    35  		Name:    "hack",
    36  		Exec:    hack.Exec,
    37  		FlagSet: fs,
    38  		Options: []ff.Option{
    39  			ff.WithEnvVarNoPrefix(),
    40  		},
    41  	}, hack
    42  }
    43  
    44  func (h *Hack) Exec(_ context.Context, _ []string) error {
    45  	flag.Usage()
    46  	return nil
    47  }
    48  
    49  // AfterParse is ran after ffcli.Parse is called with an instance of Hack.
    50  func (h *Hack) AfterParse() error {
    51  	cwd, err := bazel.ResolveWd()
    52  	if err != nil {
    53  		return fmt.Errorf("failed to resolve current working directory: %w", err)
    54  	}
    55  	if bazel.IsBazelRun() {
    56  		if err := os.Chdir(cwd); err != nil {
    57  			return err
    58  		}
    59  	}
    60  
    61  	reporoot, err := bazel.FindRepoRoot(cwd)
    62  	if err != nil {
    63  		return fmt.Errorf("failed to find repository root: %w", err)
    64  	}
    65  
    66  	h.Paths = Paths{RepoRoot: reporoot, WorkingDir: cwd}
    67  
    68  	return nil
    69  }
    70  

View as plain text