Skip to content

Commit

Permalink
Update ProjectCommand internals
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigel2392 committed May 4, 2024
1 parent 5ed8324 commit 1eb4a8c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ QuickGo is a simple and easy to use golang command line tool for creating projec
quickgo can be installed using the following command:

```bash
go install github.com/Nigel2392/quickgo/[email protected].2
go install github.com/Nigel2392/quickgo/[email protected].3
```

# Usage
Expand Down
28 changes: 21 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,15 @@ func main() {
name = fmt.Sprintf("%s=%s", name, f.DefValue)
}

fmt.Printf(" -%s\n", quickgo.Craft(quickgo.CMD_Cyan, name))
fmt.Printf(" %s: %s\n", f.Name, f.Usage)
fmt.Printf(
" -%s: %s\n",
quickgo.BuildColorString(
quickgo.CMD_Cyan,
quickgo.CMD_Bold,
name,
),
f.Usage,
)
})

// Try to load the project configuration.
Expand All @@ -155,11 +162,14 @@ func main() {
} else if err == nil {

// Project found, commands is map -> sort to slice.
var commands = make([]string, 0, len(qg.ProjectConfig.Commands))
for k := range qg.ProjectConfig.Commands {
commands = append(commands, k)
var commands = make([]*config.ProjectCommand, 0, len(qg.ProjectConfig.Commands))
for _, v := range qg.ProjectConfig.Commands {
commands = append(commands, v)
}
slices.Sort(commands)

slices.SortFunc(commands, func(a, b *config.ProjectCommand) int {
return strings.Compare(a.Name, b.Name)
})

fmt.Println(
quickgo.Craft(
Expand All @@ -168,7 +178,11 @@ func main() {
),
)
for _, c := range commands {
fmt.Printf(" - %s\n", quickgo.Craft(quickgo.CMD_Cyan, c))
if c.Description == "" {
fmt.Printf(" - %s\n", quickgo.Craft(quickgo.CMD_Cyan, c.Name))
continue
}
fmt.Printf(" - %s: %s", quickgo.Craft(quickgo.CMD_Cyan, c.Name), c.Description)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion quickgo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ afterCopy:
- $projectPath
commands:
echoName:
description: Echo Project Name
args:
customProjectName: $Data
customProjectName1:
steps:
steps:
- name: Echo Project Name
Expand Down
31 changes: 24 additions & 7 deletions quickgo/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/Nigel2392/quickgo/v2/quickgo/logger"
"github.com/Nigel2392/quickgo/v2/quickgo/quickfs"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -68,9 +69,9 @@ type (
Context map[string]any `yaml:"context" json:"context"`

// List of commands to run
BeforeCopy *command.StepList `yaml:"beforeCopy" json:"beforeCopy"`
AfterCopy *command.StepList `yaml:"afterCopy" json:"afterCopy"`
Commands map[string]*ProjectCommand `yaml:"commands" json:"commands"` // [name] => [steps]
BeforeCopy *command.StepList `yaml:"beforeCopy" json:"beforeCopy"`
AfterCopy *command.StepList `yaml:"afterCopy" json:"afterCopy"`
Commands ProjectCommandMap `yaml:"commands" json:"commands"` // [name] => [steps]

// Variable delimiters for the project templates.
DelimLeft string `yaml:"delimLeft" json:"delimLeft"`
Expand All @@ -87,7 +88,8 @@ type (
ProjectCommand struct {
// The name of the command.
// Only used internally for logging purposes.
name string `yaml:"-" json:"-"`
Name string `yaml:"-" json:"-"`
Description string `yaml:"description" json:"description"`
// Args are the arguments to pass to the command.
// These will be asked via stdin if not provided.
Args map[string]any `yaml:"args" json:"args"`
Expand All @@ -96,6 +98,20 @@ type (
}
)

type ProjectCommandMap map[string]*ProjectCommand

func (p *ProjectCommandMap) UnmarshalYAML(value *yaml.Node) error {
var commands = make(map[string]*ProjectCommand)
if err := value.Decode(&commands); err != nil {
return err
}
for k, v := range commands {
v.Name = k
}
*p = commands
return nil
}

func (e ErrorStr) Error() string {
return string(e)
}
Expand Down Expand Up @@ -147,8 +163,9 @@ func ExampleProjectConfig() *Project {
},
},
},
Commands: map[string]*ProjectCommand{
Commands: ProjectCommandMap{
"echoName": {
Description: "Echo the project name, supply one with customProjectName=myValue",
Args: map[string]any{
"customProjectName": "$projectName",
},
Expand Down Expand Up @@ -185,7 +202,7 @@ func (p *Project) Command(name string, context map[string]any) (*ProjectCommand,
cmd.Args = make(map[string]any)
}

cmd.name = name
cmd.Name = name
cmd.Args["projectName"] = p.Name
cmd.Args["projectPath"], _ = os.Getwd()

Expand Down Expand Up @@ -262,7 +279,7 @@ func (c *ProjectCommand) Execute(env map[string]any) error {

var jsonData, err = json.MarshalIndent(newEnv, "", " ")
if err == nil {
logger.Debugf("Running command '%s' with environment: %s", c.name, jsonData)
logger.Debugf("Running command '%s' with environment: %s", c.Name, jsonData)
} else {
logger.Warnf("Error marshalling environment map for logging: %v", err)
}
Expand Down
9 changes: 9 additions & 0 deletions quickgo/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ func Craft(color, s any) string {
return fmt.Sprintf("%s%v%s", color, s, CMD_Reset)
}

func BuildColorString(colors ...string) string {
var s strings.Builder
for _, color := range colors {
s.WriteString(color)
}
s.WriteString(CMD_Reset)
return s.String()
}

func PrintLogo() {
// Quick GO logo.
str := Craft(CMD_Cyan, " $$$$$$\\ $$\\ $$\\ "+Craft(CMD_Cyan, " $$$$$$\\\n")) +
Expand Down

0 comments on commit 1eb4a8c

Please sign in to comment.