Skip to content

Commit

Permalink
Updated output formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
davesavic committed Jan 1, 2024
1 parent cf72c45 commit 9d70de2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
37 changes: 37 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
},
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"
"log"
"os"
"time"
)

// runCmd represents the run command
Expand Down Expand Up @@ -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"))
}
},
}
Expand Down
23 changes: 9 additions & 14 deletions ploy/ploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"os/exec"
"strings"
"time"
)

type Params map[string]string
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -129,19 +119,24 @@ 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
}

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
Expand Down

0 comments on commit 9d70de2

Please sign in to comment.