Skip to content

Commit

Permalink
cmd/lifectl: implement start and stop commands
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Nov 13, 2023
1 parent 3062082 commit cf03b39
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
31 changes: 26 additions & 5 deletions cmd/lifectl/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"fmt"
"log"
"os"
"strconv"

"github.com/urfave/cli/v2"
)
Expand All @@ -17,10 +19,22 @@ var (

func main() {
startCmd := &cli.Command{
Name: "start",
Usage: "Starts a cloudlife application",
Name: "start",
Usage: "Starts a cloudlife application",
UsageText: "lifectl start [size of multiverse]",
Action: func(cCtx *cli.Context) error {
startMultiverse()
size := 4
if cCtx.NArg() > 0 {
size, _ = strconv.Atoi(cCtx.Args().Get(0))
}

result, err := startMultiverse(size)
if err != nil {
fmt.Println(err)
return nil
}

fmt.Println(result)
return nil
},
}
Expand All @@ -29,14 +43,19 @@ func main() {
Name: "stop",
Usage: "Stops a cloudlife application",
Action: func(cCtx *cli.Context) error {
stopMultiverse()
result, err := stopMultiverse()
if err != nil {
fmt.Println(err)
return nil
}
fmt.Println(result)
return nil
},
}

runCmd := &cli.Command{
Name: "run",
Usage: "Runs a cloudlife application",
Usage: "Runs the cloudlife application",
Action: func(cCtx *cli.Context) error {
runMultiverse()
return nil
Expand All @@ -52,6 +71,8 @@ func main() {
Destination: &host,
},
},
Name: "lifectl",
Usage: "CLI for cloudlife",
UsageText: "lifectl [global options] command [command options] [arguments]",
Commands: []*cli.Command{startCmd, runCmd, stopCmd},
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/lifectl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"io"
"net/http"
"net/url"
"strconv"
)

func startMultiverse() (string, error) {
func startMultiverse(size int) (string, error) {
pth, _ := url.JoinPath(host, "multiverse")
pth += "?n=" + strconv.Itoa(size)
resp, err := http.Post(pth, "", nil)
if err != nil {
return "", err
Expand Down
31 changes: 30 additions & 1 deletion cmd/lifectl/stop.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
package main

func stopMultiverse() {
import (
"errors"
"io"
"net/http"
"net/url"
)

func stopMultiverse() (string, error) {
url, _ := url.JoinPath(host, "multiverse")
req, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
return "", err
}

client := &http.Client{}
r, err := client.Do(req)
if err != nil {
return "", err
}
defer r.Body.Close()

if r.StatusCode != http.StatusOK {
return "", errors.New("failed to delete multiverse")
}

body, err := io.ReadAll(r.Body)
if err != nil {
return "", err
}

return string(body), nil
}

0 comments on commit cf03b39

Please sign in to comment.