Skip to content

internal/command: merge maybeGetLanguageRepo, maybeLoadStateAndConfig, and execute into a single Run function #194

@julieqiu

Description

@julieqiu

At the moment, the call stack for librarian is:

main --> librarian.Run --> command.RunCommand --> maybeGetLanguageRepo + maybeLoadStateAndConfig + execute

This creates a level of abstraction that requires all commands to follow the abstraction of maybeGetLanguageRepo + maybeLoadStateAndConfig + execute, which isn't always applicable (at the moment, it only applies to 5 out of the 7 existing commands).

To eliminate the command.RunCommand abstraction, change the current Command struct:

type Command struct {
	// Name is the unique identifier for the command.
	Name string

	// Short is a concise description shown in the 'librarian -h' output.
	Short string

	// maybeGetLanguageRepo attempts to obtain a language-specific Git
	// repository, cloning if necessary.  Returns nil if not applicable.
	maybeGetLanguageRepo func(workRoot string) (*gitrepo.Repo, error)

	// maybeLoadStateAndConfig attempts to load pipeline state and config, even if no
	// language repo is present.
	maybeLoadStateAndConfig func(languageRepo *gitrepo.Repo) (*statepb.PipelineState, *statepb.PipelineConfig, error)

	// execute runs the command's with the provided context.
	execute func(*commandState) error

	// flagFunctions are functions to initialize the command's flag set.
	flagFunctions []func(fs *flag.FlagSet)

	// flags is the command's flag set for parsing arguments and generating
	// usage messages. This is populated for each command in init().
	flags *flag.FlagSet
}

To:

type Command struct {
	// Name is the unique identifier for the command.
	Name string

	// Short is a concise description shown in the 'librarian -h' output.
	Short string

	// Run executes the command.
	Run func(ctx context.Context) error

	// flagFunctions are functions to initialize the command's flag set.
	flagFunctions []func(fs *flag.FlagSet)

	// flags is the command's flag set for parsing arguments and generating
	// usage messages. This is populated for each command in init().
	flags *flag.FlagSet
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions