forked from koding/kite
/
run.go
68 lines (53 loc) · 1.29 KB
/
run.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
package cmd
import (
"errors"
"os"
"path/filepath"
"strings"
"syscall"
"github.com/koding/kite/kitekey"
)
type Run struct{}
func NewRun() *Run {
return &Run{}
}
func (*Run) Definition() string {
return "Run a kite"
}
func (*Run) Exec(args []string) error {
// Parse kite name
if len(args) == 0 {
return errors.New("You should give a kite name")
}
// User is allowed to enter kite name in these forms: "fs" or "github.com/koding/fs.kite/1.0.0"
suppliedName := args[0]
installedKites, err := getInstalledKites(suppliedName)
if err != nil {
return err
}
var matched []*InstalledKite
for _, ik := range installedKites {
if strings.TrimSuffix(ik.Repo, ".kite") == strings.TrimSuffix(suppliedName, ".kite") {
matched = append(matched, ik)
}
}
if len(matched) == 0 {
for _, ik := range installedKites {
if ik.String() == suppliedName {
matched = append(matched, ik)
}
}
}
if len(matched) == 0 {
return errors.New("Kite not found")
}
if len(matched) > 1 {
return errors.New("More than one version is installed. Please give a full kite name as: domain/user/repo/version")
}
kiteHome, err := kitekey.KiteHome()
if err != nil {
return err
}
binPath := filepath.Join(kiteHome, "kites", matched[0].BinPath())
return syscall.Exec(binPath, args, os.Environ())
}