diff --git a/cmd/init.go b/cmd/init.go index aeed2bf..d986d49 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -4,7 +4,11 @@ Copyright © 2023 Dave Savic package cmd import ( + "encoding/json" + "github.com/davesavic/ploy/ploy" "github.com/spf13/cobra" + "log" + "os" ) // initCmd represents the init command @@ -13,7 +17,40 @@ var initCmd = &cobra.Command{ Short: "Initialise a ploy script", Long: `Initialise a ploy script`, Run: func(cmd *cobra.Command, args []string) { + cfg := ploy.Config{ + Params: map[string]string{ + "message": "hello, world!", + }, + Servers: map[string]ploy.Server{ + "staging": { + Host: "111.111.111.111", + Port: 22, + User: "ploy", + PrivateKey: "/home/user/.ssh/id_rsa", + }, + }, + Tasks: map[string][]string{ + "print-message": { + "echo '{{message}}'", + }, + }, + Pipelines: map[string]ploy.Pipeline{ + "say-hello": { + Tasks: []string{ + "print-message", + }, + Servers: []string{ + "staging", + }, + }, + }, + } + jsCfg, _ := json.MarshalIndent(cfg, "", " ") + err := os.WriteFile("configuration.json", jsCfg, 0666) + if err != nil { + log.Fatal(err) + } }, } diff --git a/cmd/run.go b/cmd/run.go index 81f85c1..b7079ed 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -10,6 +10,7 @@ import ( "github.com/spf13/cobra" "log" "os" + "time" ) // runCmd represents the run command @@ -48,7 +49,7 @@ var runCmd = &cobra.Command{ log.Fatal(err) } - fmt.Println(out) + fmt.Printf("%s\n"+out, time.Now().UTC().Format("2006-01-02 15:04:05")) } }, } diff --git a/ploy/ploy.go b/ploy/ploy.go index 0e5d7b8..4b3e144 100644 --- a/ploy/ploy.go +++ b/ploy/ploy.go @@ -8,7 +8,6 @@ import ( "os" "os/exec" "strings" - "time" ) type Params map[string]string @@ -38,16 +37,6 @@ type Config struct { Pipelines Pipelines `json:"pipelines"` } -func (c *Config) HasTask(task string) bool { - _, exists := c.Tasks[task] - return exists -} - -func (c *Config) HasRollbackTask(task string) bool { - _, exists := c.Tasks[fmt.Sprintf("rollback-%s", task)] - return exists -} - type PipelineExecutor interface { Execute(pipeline string) (string, error) } @@ -88,7 +77,7 @@ func (r *RemotePipelineExecutor) Execute(pipeline string) (string, error) { Auth: []ssh.AuthMethod{ ssh.PublicKeys(signer), }, - HostKeyCallback: ssh.InsecureIgnoreHostKey(), // Change for production + HostKeyCallback: ssh.InsecureIgnoreHostKey(), //Update for production } client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", server.Host, server.Port), &sshCfg) @@ -103,6 +92,7 @@ func (r *RemotePipelineExecutor) Execute(pipeline string) (string, error) { } for _, c := range commands { + populatePlaceholders(&c, r.Config.Params) session, err := client.NewSession() if err != nil { return "", fmt.Errorf("error creating SSH session: %v", err) @@ -129,6 +119,12 @@ func (r *RemotePipelineExecutor) Execute(pipeline string) (string, error) { return out.String(), nil } +func populatePlaceholders(s *string, params Params) { + for k, v := range params { + *s = strings.ReplaceAll(*s, fmt.Sprintf("{{%s}}", k), v) + } +} + type LocalPipelineExecutor struct { Config Config } @@ -136,12 +132,11 @@ type LocalPipelineExecutor struct { func (l *LocalPipelineExecutor) Execute(pipeline string) (string, error) { var out bytes.Buffer - now := time.Now() pl := l.Config.Pipelines[pipeline] for _, t := range pl.Tasks { for _, c := range l.Config.Tasks[t] { - c = strings.ReplaceAll(c, "{{timestamp}}", now.Format("20060102150405")) + populatePlaceholders(&c, l.Config.Params) cmd := exec.Command("sh", "-c", c) cmd.Stdout = &out cmd.Stderr = &out