Skip to content

Commit

Permalink
feat: implemented create command
Browse files Browse the repository at this point in the history
  • Loading branch information
gchiesa committed Jul 12, 2024
1 parent 4fe79e2 commit 5911e29
Show file tree
Hide file tree
Showing 42 changed files with 581 additions and 377 deletions.
49 changes: 49 additions & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cmd

import (
"github.com/apex/log"
"github.com/gchiesa/ska/internal/configuration"
"github.com/gchiesa/ska/internal/content_provider"
"github.com/gchiesa/ska/internal/processor"
)

type CreateCmd struct {
TemplateURI string `arg:"-t,--template,required" help:"URI of the template"`
DestinationPath string `arg:"-d,--destination,required" help:"Destination path"`
Variables map[string]string `arg:"-v,separate" help:"Variables to use in the template. Can be specified multiple times"`
}

func (c *CreateCmd) Execute() error {
templateProvider, err := content_provider.ContentProviderByURI(c.TemplateURI)
defer templateProvider.RemoveWorkingDir()

if err != nil {
log.Fatalf("error creating template provider: %v", err)
}

configService := configuration.NewConfigService()

if err := templateProvider.DownloadContent(); err != nil {
log.Fatalf("error downloading template: %v", err)
}

fileTreeProcessor := processor.NewFileTreeProcessor(templateProvider.WorkingDir(), c.DestinationPath, processor.TreeRendererOptions{})
defer fileTreeProcessor.RemoveWorkingDir()

vars := mapStringToMapInterface(c.Variables)
if err := fileTreeProcessor.Render(vars); err != nil {
log.Fatalf("error rendering template: %v", err)
}

// save the config
err = configService.
WithVariables(vars).
WithBlueprintUpstream(templateProvider.RemoteURI()).
WriteConfig(c.DestinationPath)
if err != nil {
log.Fatalf("error writing config: %v", err)
}

log.Infof("template created under file path: %s", c.DestinationPath)
return nil
}
40 changes: 40 additions & 0 deletions cmd/entrypoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmd

import (
"fmt"
"github.com/alexflint/go-arg"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"os"
)

const programName = "ska"
const version = "development"

type arguments struct {
CreateCmd *CreateCmd `arg:"subcommand:create"`
Debug bool `arg:"-d"`
}

func (arguments) Version() string { return fmt.Sprintf("%s version %s", programName, version) }

func Execute() error {
log.SetHandler(cli.New(os.Stderr))
log.SetLevel(log.InfoLevel)

var args arguments
arg.MustParse(&args)

if args.Debug {
log.SetLevel(log.DebugLevel)
}

switch {
case args.CreateCmd != nil:
return args.CreateCmd.Execute()
default:
fmt.Println("no subcommand specified")
}

return nil
}
71 changes: 0 additions & 71 deletions cmd/example.go

This file was deleted.

60 changes: 0 additions & 60 deletions cmd/example_test.go

This file was deleted.

44 changes: 0 additions & 44 deletions cmd/man.go

This file was deleted.

31 changes: 0 additions & 31 deletions cmd/root.go

This file was deleted.

19 changes: 0 additions & 19 deletions cmd/root_test.go

This file was deleted.

4 changes: 4 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package cmd

type UpdateCmd struct {
}
9 changes: 9 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cmd

func mapStringToMapInterface(m map[string]string) map[string]interface{} {
res := make(map[string]interface{})
for k, v := range m {
res[k] = v
}
return res
}
19 changes: 0 additions & 19 deletions cmd/version.go

This file was deleted.

26 changes: 0 additions & 26 deletions cmd/version_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// commitlint.config.js
// commitlint.configuration.js
module.exports = {
extends: ['@commitlint/config-conventional'],
extends: ['@commitlint/configuration-conventional'],
ignores: [(message) => /^Bumps \[.+]\(.+\) from .+ to .+\.$/m.test(message)],
}
Loading

0 comments on commit 5911e29

Please sign in to comment.