-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
159 lines (129 loc) · 4.18 KB
/
config.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
lua "github.com/yuin/gopher-lua"
)
type ConfigParser struct {
configFile string
tasks []Task
luaState *lua.LState
logger *Logger
}
func NewConfigParser(configPath string) (*ConfigParser, error) {
configFile := configPath
if !strings.HasSuffix(configPath, ".lua") {
configFile = filepath.Join(configPath, "dosh.lua")
}
logger := GetLogger()
luaState := lua.NewState()
luaState.PreloadModule("dosh_commands", DoshLuaLoader)
if fileExists(configFile) {
logger.logDefault(fmt.Sprintf("Using config file: %s", configFile))
if err := luaState.DoFile(configFile); err != nil {
panic(err)
}
}
return &ConfigParser{configFile: configFile, tasks: globalTasks, logger: logger, luaState: luaState}, nil
}
func (cp *ConfigParser) close() {
defer cp.luaState.Close()
}
func (cp *ConfigParser) init() error {
if fileExists(cp.configFile) {
return fmt.Errorf("Config file already exists: %s", cp.configFile)
}
file, err := os.Create(cp.configFile)
if err != nil {
return err
}
_, err = file.WriteString(`
local cmd = require("dosh_commands") -- main module to run DOSH commands.
local name = "there" -- you can use all features of Lua programming language.
local function hello(there) -- even you can define your custom functions.
there = there or name
local message = "Hello, " .. there .. "!"
cmd.run("osascript -e 'display notification \"" .. message .. "\" with title \"Hi!\"'")
end
cmd.add_task{ -- cmd comes from dosh.
name="hello", -- task name, or subcommand for your cli.
description="say hello", -- task description for the help output.
required_commands={"osascript"}, -- check if the programs exist before running the task.
required_platforms={"macos"}, -- check if the current operating system is available to run the task.
environments={"development", "staging"}, -- DOSH_ENV variable should be either development or staging to run this task.
command=hello -- run hello function with its parameters when the task ran.
}
`)
if err != nil {
return err
}
defer file.Close()
return nil
}
func (cp *ConfigParser) getTasks() []Task {
return cp.tasks
}
func (cp *ConfigParser) getDescription() string {
description := os.Getenv("HELP_DESCRIPTION")
if description == "" {
description = "dosh - shell-independent task manager"
}
return description
}
func (cp *ConfigParser) getEpilog() string {
return os.Getenv("HELP_EPILOG")
}
func (cp *ConfigParser) runTask(args []string) error {
if len(args) == 0 {
return fmt.Errorf("no task specified")
}
taskName := args[0]
for _, task := range cp.tasks {
if task.Name == taskName {
cp.logger.logDefault("Running task: " + task.Name)
task.run(cp.luaState)
return nil
}
}
return fmt.Errorf("Task not found: %s", taskName)
}
func (cp *ConfigParser) generateHelpOutput() string {
helpOutput := []string{}
description := cp.getDescription()
if description != "" {
helpOutput = append(helpOutput, description, "")
}
tasks := cp.getTasks()
if len(tasks) > 0 {
helpOutput = append(helpOutput, "Tasks:")
for _, task := range tasks {
helpOutput = append(helpOutput, fmt.Sprintf(" > %-20s %s", task.Name, task.Description))
}
helpOutput = append(helpOutput, "")
}
helpOutput = append(helpOutput,
"DOSH commands:",
" > help print this output",
" > init initialize a new config in current working directory",
" > version print version of DOSH",
"",
" -c string specify config path (default: dosh.lua)",
" -d string change the working directory",
" -v int increase the verbosity of messages:",
" 1 - default, 2 - detailed, 3 - debug",
)
epilog := cp.getEpilog()
if epilog != "" {
helpOutput = append(helpOutput, "", epilog)
}
return strings.Join(helpOutput, "\n")
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if err != nil {
return !os.IsNotExist(err)
}
return !info.IsDir()
}