Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ type Contract interface {
BuildCommand(path string, info fs.DirEntry, parent Command, d DiscoveryContext) (Command, error)
}

// ContractReporter is implemented by Commands that know which Contract
// discovered them. Reflect on a discovered Command to learn its contract:
//
// if r, ok := cmd.(ContractReporter); ok {
// fmt.Println(r.Contract()) // e.g. "OpenCLI"
// }
type ContractReporter interface {
// Contract returns the name of the Contract that built the Command.
Contract() string
}

// ErrNotApplicable indicates that a contract does not apply to a given file/directory.
// Discovery will try the next contract in the list.
var ErrNotApplicable = errors.New("contract does not apply")
Expand Down
1 change: 1 addition & 0 deletions contract_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (c *DirectoryContract) BuildCommand(path string, info fs.DirEntry, parent C
discoveredIn: filepath.Dir(path),
executor: d.Executor(),
cache: d.Cache(),
contract: "Directory",
},
discoverer: d.Next(),
}, nil
Expand Down
2 changes: 2 additions & 0 deletions contract_executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (c *ExecutableContract) BuildCommand(path string, info fs.DirEntry, parent
discoveredIn: filepath.Dir(path),
executor: d.Executor(),
cache: d.Cache(),
contract: "Executable",
}, nil
}

Expand All @@ -56,5 +57,6 @@ func (c *ExecutableContract) BuildCommand(path string, info fs.DirEntry, parent
executor: d.Executor(),
cache: d.Cache(),
discoverer: d.Next(),
contract: "Executable",
}, nil
}
2 changes: 2 additions & 0 deletions contract_opencli.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (c *OpenCLIContract) BuildCommand(path string, info fs.DirEntry, parent Com
discoveredIn: filepath.Dir(path),
executor: d.Executor(),
cache: d.Cache(),
contract: "OpenCLI",
}, nil
}

Expand All @@ -51,6 +52,7 @@ func (c *OpenCLIContract) BuildCommand(path string, info fs.DirEntry, parent Com
cache: d.Cache(),
discoverer: d.Next(),
describe: describeOpenCLI,
contract: "OpenCLI",
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions contract_shell_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (c *ShellScriptContract) BuildCommand(path string, info fs.DirEntry, parent
discoveredIn: filepath.Dir(path),
executor: d.Executor(),
cache: d.Cache(),
contract: "ShellScript",
},
}, nil
}
Expand Down
1 change: 1 addition & 0 deletions contract_standalone_executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (c *StandaloneExecutableContract) BuildCommand(path string, info fs.DirEntr
discoveredIn: filepath.Dir(path),
executor: d.Executor(),
cache: d.Cache(),
contract: "StandaloneExecutable",
}

// Only applies to executables that define a summary
Expand Down
31 changes: 31 additions & 0 deletions contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,37 @@ func TestDefaultCommandFromDescriptor(t *testing.T) {
assert.Equal(t, "b", parent.DefaultSubcommand().Name())
}

// TestContractReporter verifies that a Command built by a Contract reports the
// name of that Contract via the ContractReporter interface.
func TestContractReporter(t *testing.T) {
d := &discoverer{maxDepth: -1, executor: defaultExecutor, cache: nullCache{}}

scenarios := []struct {
contract Contract
executable string
expected string
}{
{&OpenCLIContract{}, "opencli-tool", "OpenCLI"},
{&ShellScriptContract{}, "echoargs", "ShellScript"},
{&ExecutableContract{}, "go.exoskeleton", "Executable"},
{&StandaloneExecutableContract{}, "hello", "StandaloneExecutable"},
{&DirectoryContract{MetadataFilename: ".exoskeleton"}, "nested-1", "Directory"},
}

for _, s := range scenarios {
path := filepath.Join(fixtures, s.executable)
info, err := os.Lstat(path)
assert.NoErrorf(t, err, "Given executable=%s", s.executable)

cmd, err := s.contract.BuildCommand(path, fs.FileInfoToDirEntry(info), nil, d)
assert.NoErrorf(t, err, "Given executable=%s", s.executable)

reporter, ok := cmd.(ContractReporter)
assert.Truef(t, ok, "%s should implement ContractReporter", s.executable)
assert.Equalf(t, s.expected, reporter.Contract(), "Given executable=%s", s.executable)
}
}

// TestWithContractsOption verifies that WithContracts replaces the contracts.
func TestWithContractsOption(t *testing.T) {
// Create a custom contract that only matches files named "custom"
Expand Down
3 changes: 3 additions & 0 deletions discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func TestDiscovererBuildsCommand(t *testing.T) {
path: filepath.Join(fixtures, "echoargs"),
discoveredIn: fixtures,
cache: nullCache{},
contract: "ShellScript",
},
},
},
Expand All @@ -144,6 +145,7 @@ func TestDiscovererBuildsCommand(t *testing.T) {
path: filepath.Join(fixtures, "nested-1", ".exoskeleton"),
discoveredIn: fixtures,
cache: nullCache{},
contract: "Directory",
},
discoverer: d.Next(),
},
Expand All @@ -157,6 +159,7 @@ func TestDiscovererBuildsCommand(t *testing.T) {
discoveredIn: fixtures,
cache: nullCache{},
discoverer: d.Next(),
contract: "Executable",
},
},
}
Expand Down
3 changes: 3 additions & 0 deletions executable_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ type executableCommand struct {
discoverer DiscoveryContext
cache Cache
describe describeFunc
contract string
}

func (cmd *executableCommand) Parent() Command { return cmd.parent }
func (cmd *executableCommand) Path() string { return cmd.path }
func (cmd *executableCommand) Name() string { return cmd.name }
func (cmd *executableCommand) Aliases() []string { return cmd.aliases }
func (cmd *executableCommand) DiscoveredIn() string { return cmd.discoveredIn }
func (cmd *executableCommand) Contract() string { return cmd.contract }

// Command returns an exec.Cmd that will run the executable with the given arguments.
func (cmd *executableCommand) Command(args ...string) *exec.Cmd {
Expand Down Expand Up @@ -188,6 +190,7 @@ func toCommands(parent *executableCommand, descriptors []*commandDescriptor, arg
defaultSubcommand: descriptor.DefaultCommand,
executor: parent.executor,
cache: parent.cache,
contract: parent.contract,
}

if len(descriptor.Commands) > 0 && d.MaxDepth() != 0 {
Expand Down
Loading