Skip to content

Commit

Permalink
feat(cmd): Add build Command to CLI Tool
Browse files Browse the repository at this point in the history
  • Loading branch information
jshlbrd committed Oct 5, 2024
1 parent 6cae3cd commit 1073364
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
108 changes: 108 additions & 0 deletions cmd/substation/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(buildCmd)
buildCmd.PersistentFlags().BoolP("recursive", "R", false, "recursively build all files")
buildCmd.PersistentFlags().StringToString("ext-str", nil, "set external variables")
}

var buildCmd = &cobra.Command{
Use: "build [path]",
Short: "build configs",
Long: `'substation build' compiles configuration files.
The 'recursive' flag can be used to build all files in a directory,
and the current directory is used if no arg is provided.`,
Example: ` substation build [-R]
substation build [-R] /path/to/configs
substation build config.jsonnet
`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// Default to current directory if no path is provided
path := "."
if len(args) > 0 {
path = args[0]
}

m, err := cmd.PersistentFlags().GetStringToString("ext-str")
if err != nil {
return err
}

r, err := cmd.Flags().GetBool("recursive")
if err != nil {
return err
}

return buildPath(path, m, r)
},
}

func buildPath(arg string, extVars map[string]string, recursive bool) error {
// Handle cases where the path is a file.
//
// Only `.jsonnet` files are built.
if filepath.Ext(arg) == ".jsonnet" {
mem, err := buildFile(arg, extVars)
if err != nil {
return err
}

dir, fname := pathVars(arg)
if err := os.WriteFile(filepath.Join(dir, fname)+".json", []byte(mem), 0o644); err != nil {
return err
}

// Print the file that was built.
fmt.Println(arg)

return nil
}

if err := filepath.WalkDir(arg, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}

if d.IsDir() {
if !recursive && path != arg {
return filepath.SkipDir
}

return nil
}

// Only `.jsonnet` files are built.
if filepath.Ext(path) != ".jsonnet" {
return nil
}

mem, err := buildFile(path, extVars)
if err != nil {
return err
}

dir, fname := pathVars(path)
if err := os.WriteFile(filepath.Join(dir, fname)+".json", []byte(mem), 0o644); err != nil {
return err
}

// Print the file that was built.
fmt.Println(path)

return nil
}); err != nil {
return err
}

return nil
}
11 changes: 11 additions & 0 deletions cmd/substation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"os"
"path/filepath"
"strings"

"github.com/google/go-jsonnet"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -42,6 +44,15 @@ func buildFile(f string, extVars map[string]string) (string, error) {
return res, nil
}

// pathVars returns the directory and file name of a file path.
func pathVars(p string) (string, string) {
dir, fn := filepath.Split(p)
ext := filepath.Ext(fn)
fn = strings.TrimSuffix(fn, ext)

return dir, fn
}

func main() {
err := rootCmd.Execute()
if err != nil {
Expand Down

0 comments on commit 1073364

Please sign in to comment.