summaryrefslogtreecommitdiff
path: root/main.go
blob: 0eddea20ec7b4aa223b64b9b9d97eb2e699d0c86 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"embed"
	"os"
	"unicode"

	"go.wit.com/lib/protobuf/argvpb"
	"go.wit.com/lib/protobuf/filepb"
	"go.wit.com/log"
)

// sent via -ldflags
var VERSION string
var BUILDTIME string

// used for shell auto completion
var ARGNAME string = "wit" // todo: get this from $0 ?

//go:embed resources/*
var resources embed.FS

func main() {
	me = new(mainType)
	// autocomplete must be processed before there is anything sent to STDOUT or STDERR
	me.sh = argvpb.Autocomplete(&argv) // adds shell auto complete to go-args
	me.homedir, _ = os.UserHomeDir()   // store shortcut here todo: add better logic

	if me.sh.Cmd == "" {
		// user didn't enter a sub command
		// doDefaultBehavior()
		me.sh.GoodExit("do what?")
	}

	pwd, _ := os.Getwd()
	setTitle(log.Sprintf("wit %s %s", me.sh.Cmd, pwd))

	// Standard subcommand handling starts here
	var s string
	var err error

	if argv.Upgrade != nil {
		doUpgrade()
		me.sh.GoodExit("")
	}

	if argv.Linux != nil {
		if argv.Linux.Rdate != nil {
			s, err = doRdate()
		} else {
			setTerminalTitle("pinging google", "ping", []string{"google.com"})
		}
	}

	if argv.Build != nil {
		s, err = doBuild()
	}

	if argv.Git != nil {
		initMachine()
		s, err = doGit()
	}

	if argv.PB != nil {
		pbuuid, pbver, pberr := filepb.IdentifyPB(argv.PB.Identify)
		if pberr == nil {
			log.Info("pb version is:", pbver)
			log.Info("pb uuid    is:", pbuuid)
			s = "pb identify worked"
		} else {
			s = "identify failed"
			err = pberr
		}
	}

	if argv.Clone != nil {
		doClone()
		me.sh.GoodExit("")
	}

	if argv.WITCOM {
		doWITCOM()
		me.sh.GoodExit("")
	}

	if argv.Clone != nil {
		doClone()
		me.sh.GoodExit("")
	}

	if argv.WITCOM {
		doWITCOM()
		me.sh.GoodExit("")
	}

	if argv.Upgrade != nil {
		doUpgrade()
	}

	if argv.Droplet != nil {
		s, err = doDroplet()
	}

	if argv.Publish != nil {
		if err := doPublish(); err != nil {
			me.sh.BadExit("doPublish failed", err)
		}
		me.sh.GoodExit("")
	}

	if argv.Zoo != nil {
		if areSuperuser() {
			exitOnErrorRealtime([]string{"journalctl", "-n", "100", "-f", "_SYSTEMD_UNIT=zood.service"})
		}
		me.sh.GoodExit("do something here")
	}
	if err != nil {
		me.sh.BadExit(s, err)
	}
	me.sh.GoodExit(s)
}

// this is dumb. sync this with go-deb
func trimNonNumericFromStart(s string) string {
	for i, r := range s {
		if unicode.IsDigit(r) {
			return s[i:]
		}
	}
	return ""
}