-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
93 lines (76 loc) · 1.97 KB
/
cli.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
package main
import (
"flag"
"fmt"
"os"
"strconv"
"time"
"github.com/emaincourt/codeship-cli/graphics"
"github.com/emaincourt/codeship-cli/providers"
"github.com/gizak/termui"
)
func main() {
organization := flag.String("org", "", "The name of the organization.")
username := flag.String("username", os.Getenv("CODESHIP_USERNAME"), "Codeship username.")
password := flag.String("password", os.Getenv("CODESHIP_PASSWORD"), "Codeship password.")
flag.Parse()
if *username == "" {
panic(fmt.Errorf("username must be provided either from env var CODESHIP_USERNAME or --username"))
}
if *password == "" {
panic(fmt.Errorf("password must be provided either from env var CODESHIP_PASSWORD or --password"))
}
provider, err := providers.NewCodeShipProviderFromCredentials(*username, *password, *organization)
if err != nil {
panic(err)
}
termUI, err := graphics.NewTerm()
if err != nil {
panic(err)
}
err = termUI.Start()
if err != nil {
panic(err)
}
defer termUI.Close()
header, err := provider.GetHeader()
if err != nil {
panic(err)
}
termUI.SetHeader(header)
projects, err := provider.GetProjectsList()
if err != nil {
panic(err)
}
termUI.SetProjectsList(projects)
latestKBInput := time.Now()
currentProjectIndex := 0
termUI.AddTimeLoop(func(e termui.Event) {
currentProjectID, err := provider.GetProjectIDFromIndex(currentProjectIndex)
if err != nil {
panic(err)
}
builds, err := provider.GetBuildsList(currentProjectID)
if err != nil {
panic(err)
}
termUI.SetBuildsList(builds)
termUI.Clear()
termUI.Render()
})
termUI.AddKeyPressHandler(func(e termui.Event) {
if time.Now().Sub(latestKBInput) < 1000000000 {
i, err := strconv.Atoi(strconv.Itoa(currentProjectIndex) + e.Data.(termui.EvtKbd).KeyStr)
if err == nil {
currentProjectIndex = i
}
} else {
i, err := strconv.Atoi(e.Data.(termui.EvtKbd).KeyStr)
if err == nil {
currentProjectIndex = i
}
}
latestKBInput = time.Now()
})
termUI.Loop()
}