Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
migsc committed Jan 14, 2025
1 parent 60364b5 commit 80b5516
Show file tree
Hide file tree
Showing 12 changed files with 577 additions and 433 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/node_modules/
/lib/
/dist/
/docs/
.idea/*

.DS_Store
Expand Down Expand Up @@ -40,4 +39,7 @@ dist
publish

# Mystery files I haven't yet gone through and don't want cluttering up the codebase
.misc
.misc

# VSCode
.code-workspace
1 change: 1 addition & 0 deletions args/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,4 @@ func validateNone(cobraCmd *cobra.Command, ruleDefs []types.ArgRuleDef, args []s

return nil
}

28 changes: 15 additions & 13 deletions bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,22 @@ func copyIncludedFile(includedFilePath string, targetDir string) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

// If the file is executable for user, group, or others, log it
// If the file is executable for user, group, or others, throw an error
if fileInfo.Mode()&0111 != 0 {
cmd2 := exec.Command("chmod", "+x", filepath.Join(targetDir, filepath.Base(expandedPath)))
cmd2.Stdout = os.Stdout
cmd2.Stderr = os.Stderr
log.Info("copying executable file",
"file", includedFilePath,
"mode", fileInfo.Mode().String(),
)
err = cmd.Run()
if err != nil {
return err
}
return cmd2.Run() // TODO: This is not working. Could we move it to the binary directory and then run it from there?
return fmt.Errorf("cannot include executable file in the bundle: %s", expandedPath)
// Previous implementation where we wanted to allow executable files in the bundle
// cmd2 := exec.Command("chmod", "+x", filepath.Join(targetDir, filepath.Base(expandedPath)))
// cmd2.Stdout = os.Stdout
// cmd2.Stderr = os.Stderr
// log.Info("copying executable file",
// "file", includedFilePath,
// "mode", fileInfo.Mode().String(),
// )
// err = cmd.Run()
// if err != nil {
// return err
// }
// return cmd2.Run() // TODO: This is not working. Could we move it to the binary directory and then run it from there?
} else {
return cmd.Run()
}
Expand Down
14 changes: 14 additions & 0 deletions bundle/main.template.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,20 @@ func setupCommand(cmdConfig *types.CmdeagleConfig, commandDef *types.CommandDefi
script = flagStore.Interpolate(script)
script = paramsStore.Interpolate(script)

// TODO: Useful?
// cmdVisitor.paramStore.Set("LOG_LEVEL", "debug")
paramsStore.Set("cli.name", cmdConfig.Name)
// cmdVisitor.paramStore.Set("CMD_VERSION", cmdConfig.Version)
// cmdVisitor.paramStore.Set("CMD_DESCRIPTION", cmdConfig.Description)
// cmdVisitor.paramStore.Set("CMD_AUTHOR", cmdConfig.Author)
// cmdVisitor.paramStore.Set("CMD_LICENSE", cmdConfig.License)
binDirPath, err := executable.GetDestDir()
if err != nil {
return fmt.Errorf("failed to get binary directory: %w", err)
}
paramsStore.Set("cli.bin_dir", binDirPath)
paramsStore.Set("cli.data_dir", appDataDirPath)

// 4. Run the command
log.Debug("Run / Running start script for", "path", commandPath, "script", script)

Expand Down
53 changes: 40 additions & 13 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,31 @@ func runBuild() error {
return fmt.Errorf("error writing %s: %w", outFile, err)
}

// There's no need to setup an empty directory for the binary to be built in because we expect it to be
// shared with other binaries already on the system.
var binDirPath string
binDirPath, err = executable.GetDestDir()
if err != nil {
return err
}

// Create a visitor to handle the build-specific command processing in a recursive manner.
cmdVisitor := BuildCommandVisitor{
config: cmdConfig,
baseDir: bundleStagingDirPath,
config: cmdConfig,
baseDir: bundleStagingDirPath,
envStore: envvar.CreateEnvStore(),
}

// TODO: Useful?
// cmdVisitor.paramStore.Set("LOG_LEVEL", "debug")
cmdVisitor.envStore.Set("cli.name", cmdConfig.Name)
// cmdVisitor.paramStore.Set("CMD_VERSION", cmdConfig.Version)
// cmdVisitor.paramStore.Set("CMD_DESCRIPTION", cmdConfig.Description)
// cmdVisitor.paramStore.Set("CMD_AUTHOR", cmdConfig.Author)
// cmdVisitor.paramStore.Set("CMD_LICENSE", cmdConfig.License)
cmdVisitor.envStore.Set("cli.bin_dir", binDirPath)
cmdVisitor.envStore.Set("cli.data_dir", executable.GetAppDataDir(cmdConfig.Name))

// First we build the root command by creating a command definition from the root configuration.
rootCommandDef := &types.CommandDefinition{
Name: cmdConfig.Name,
Expand Down Expand Up @@ -281,14 +300,6 @@ func runBuild() error {
})
}

// There's no need to setup an empty directory for the binary to be built in because we expect it to be
// shared with other binaries already on the system.
var binDirPath string
binDirPath, err = executable.GetDestDir()
if err != nil {
return err
}

// Finally we build the binary
log.Debug("Preparing to build binary with", "binDirPath", binDirPath, "targetBinaryPath")
targetBinaryPath := filepath.Join(binDirPath, cmdConfig.Name)
Expand All @@ -311,8 +322,9 @@ func runBuild() error {
// BuildCommandVisitor handles the build-specific command processing
// during command tree traversal
type BuildCommandVisitor struct {
config *types.CmdeagleConfig
baseDir string
config *types.CmdeagleConfig
baseDir string
envStore *envvar.EnvStateStore
}

func (v *BuildCommandVisitor) Visit(commandDef *types.CommandDefinition, parent *types.CommandDefinition, path []string) error {
Expand All @@ -334,8 +346,23 @@ func (v *BuildCommandVisitor) Build(commandDef *types.CommandDefinition, parent
"script", commandDef.Build,
)

// Interpolate params such as environment variables
script := commandDef.Build
script = v.envStore.Interpolate(script)

// TODO: Highly inefficient, but it works for now

// TODO: Should probably allow the user to specify the shell to run the build script in.
cmd := exec.Command("sh", "-c", commandDef.Build)
cmd := exec.Command("sh", "-c", script)

// Copy the current environment and add new variables iteratively
envVars := v.envStore.GetEnvVariables()
cmd.Env = os.Environ() // Start with the current environment
for _, env := range envVars {
log.Debug("Run / Setting environment variable", "path", commandPath, "env", env.Name+"="+env.Value)
cmd.Env = append(cmd.Env, env.Name+"="+env.Value)
}

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

Expand Down
6 changes: 3 additions & 3 deletions cmdeagle.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"path": "."
},
{
"path": "../../lab/cli-example"
"path": "../cmdeagle-examples"
}
],
"settings": {},
}
"settings": {}
}
14 changes: 14 additions & 0 deletions config/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ type ParamsStateStore struct {
Entries map[string]string
}

func CreateEmptyParamsStore() *ParamsStateStore {
store := &ParamsStateStore{
Args: args.CreateArgsStore(nil, nil, nil),
Flags: flags.CreateFlagsStore(nil, nil),
Entries: map[string]string{},
}

return store
}

func CreateParamsStore(argsStore *args.ArgsStateStore, flagsStore *flags.FlagsStateStore) *ParamsStateStore {
store := &ParamsStateStore{
Args: argsStore,
Expand All @@ -31,6 +41,10 @@ func CreateParamsStore(argsStore *args.ArgsStateStore, flagsStore *flags.FlagsSt
return store
}

func (store *ParamsStateStore) Set(key string, value string) {
store.Entries[key] = value
}

func (store *ParamsStateStore) Interpolate(script string) string {
for key, val := range store.Entries {
placeholder := fmt.Sprintf("${%s}", key)
Expand Down
Empty file added docs/.nojekyll
Empty file.
Loading

0 comments on commit 80b5516

Please sign in to comment.