/
main.go
131 lines (115 loc) · 2.48 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/koron/netupvim/netup"
)
var (
version = "none"
)
var (
targetDir = "."
sourceName = "release"
cpu string
restore bool
selfUpdate = true
)
func setup() error {
conf, err := loadConfig("netupvim.ini")
if err != nil {
return err
}
// Parse options.
var (
helpOpt = flag.Bool("h", false, "show this message")
targetOpt = flag.String("t", conf.getTargetDir(), "target dir to upgrade/install")
sourceOpt = flag.String("s", conf.getSource(), "source of update: release,develop,canary")
restoreOpt = flag.Bool("restore", false, "force download & extract all files")
versionOpt = flag.Bool("version", false, "show version")
)
flag.Parse()
if *helpOpt {
showHelp()
os.Exit(1)
}
if *versionOpt {
showVersion()
os.Exit(1)
}
// setup context.
targetDir = *targetOpt
sourceName = *sourceOpt
restore = *restoreOpt
cpu = conf.CPU
selfUpdate = !conf.DisableSelfUpdate
netup.Version = version
netup.DownloadTimeout = conf.getDownloadTimeout()
netup.GithubUser = conf.getGithubUser()
netup.GithubToken = conf.getGithubToken()
netup.GithubVerbose = conf.GithubVerbose
if conf.LogRotateCount > 0 {
netup.LogRotateCount = conf.LogRotateCount
}
if conf.ExeRotateCount > 0 {
netup.ExeRotateCount = conf.ExeRotateCount
}
return nil
}
func shouldSelfUpdate() bool {
if !selfUpdate {
return false
}
_, err := os.Stat(filepath.Join(targetDir, "netupvim.exe"))
return err == nil
}
func run() error {
if err := setup(); err != nil {
return err
}
workDir := filepath.Join(targetDir, "netupvim")
// update vim
vimPack, ok := vimSet[sourceName]
if !ok {
return fmt.Errorf("invalid source: %s", sourceName)
}
err := netup.Update(
targetDir,
workDir,
vimPack,
netup.Arch{Name: cpu, Hint: "vim.exe"},
restore)
if err != nil {
return err
}
// try to update netupvim
if shouldSelfUpdate() {
netup.LogInfo("trying to update netupvim")
err := netup.Update(
targetDir,
workDir,
netupPack,
netup.Arch{Name: "X86"},
restore)
if err != nil {
netup.LogInfo("failed to udate netupvim: %s", err)
}
}
return nil
}
func showHelp() {
fmt.Fprintf(os.Stderr, `%[1]s is tool to upgrade/install Vim (+kaoriya) in/to target dir.
Usage: %[1]s [options]
Options are:
`, filepath.Base(os.Args[0]))
flag.PrintDefaults()
}
func showVersion() {
fmt.Fprintf(os.Stderr, "netupvim version %s\n", version)
}
func main() {
if err := run(); err != nil {
netup.LogFatal(err)
}
}