-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (93 loc) · 2.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"log"
"os"
"github.com/usecloudstate/cli/pkg/apps"
"github.com/usecloudstate/cli/pkg/client"
"github.com/usecloudstate/cli/pkg/session"
"github.com/usecloudstate/cli/pkg/setup"
"github.com/usecloudstate/cli/pkg/token"
)
type State int64
const (
SInit = iota
SClientInitiated
SSessionFail
SSessionSuccess
SUnrecoverableError
SAppCreateRequested
SSetupRequested
SSuccess
)
type StateMachineCtx struct {
client *client.Client
sesh *session.Session
ac *apps.Apps
app *apps.App
cliArgs []string
}
var Version string = "Dev"
func next(s State, ctx *StateMachineCtx) {
switch s {
case SInit:
ctx.client = client.Init()
next(SClientInitiated, ctx)
case SClientInitiated:
nsesh, err := session.Init(ctx.client)
ac := apps.Init(ctx.client)
ctx.sesh = nsesh
ctx.ac = ac
if err != nil {
log.Println("Error initiating client:", err)
next(SSessionFail, ctx)
} else {
next(SSessionSuccess, ctx)
}
case SSessionFail:
err := token.RemoveMachine()
if err != nil {
next(SUnrecoverableError, ctx)
} else {
log.Println("❌ Error initiating new session.")
next(SUnrecoverableError, ctx)
}
case SSessionSuccess:
next(SSuccess, ctx)
case SAppCreateRequested:
app, err := ctx.ac.CreateNewApp()
if err != nil {
next(SUnrecoverableError, ctx)
} else {
ctx.app = app
log.Println("💡 Hint to setup your app: $ cloudstate setup <path>")
next(SSuccess, ctx)
}
case SSetupRequested:
setup.RunSetup(ctx.ac, ctx.cliArgs[0])
case SSuccess:
return
}
}
func main() {
log.SetFlags(0)
log.Printf("Cloud State CLI (v%s)\n", Version)
argsWithoutProg := os.Args[1:]
if len(argsWithoutProg) == 0 {
log.Println("usage: $ cloudstate <command>")
return
}
cmdVar := argsWithoutProg[0]
argsWithoutCmd := argsWithoutProg[1:]
ctx := StateMachineCtx{
cliArgs: argsWithoutCmd,
}
next(SInit, &ctx)
switch cmdVar {
case "create":
next(SAppCreateRequested, &ctx)
case "setup":
next(SSetupRequested, &ctx)
default:
log.Println("Unknown command:", cmdVar)
}
}