2018-03-10 05:23:05 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-03-11 05:29:11 +08:00
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-03-10 05:23:05 +08:00
|
|
|
"os"
|
2018-03-11 05:29:11 +08:00
|
|
|
"strings"
|
2018-03-10 05:23:05 +08:00
|
|
|
|
2018-03-13 09:50:38 +08:00
|
|
|
"github.com/coreos/go-systemd/unit"
|
2018-03-10 05:23:05 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
RootCmd = &cobra.Command{
|
|
|
|
Use: "service-generator",
|
|
|
|
Short: "service-generator creates systemd Unit files",
|
|
|
|
Long: "service-generator is a convenient little tool to create systemd Unit files",
|
|
|
|
SilenceErrors: false,
|
|
|
|
SilenceUsage: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-03-12 17:10:21 +08:00
|
|
|
type Strings []string
|
|
|
|
|
|
|
|
func (s Strings) IndexOf(n string) int {
|
|
|
|
for i, v := range s {
|
|
|
|
if v == n {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Strings) Contains(n string) bool {
|
|
|
|
for _, v := range s {
|
|
|
|
if v == n {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-03-13 09:50:38 +08:00
|
|
|
func stripEmptyOptions(options []*unit.UnitOption) []*unit.UnitOption {
|
|
|
|
var opts []*unit.UnitOption
|
|
|
|
for _, opt := range options {
|
|
|
|
if len(opt.Value) > 0 {
|
|
|
|
opts = append(opts, opt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2018-03-11 05:29:11 +08:00
|
|
|
func readString(prompt string, required bool) (string, error) {
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
fmt.Printf("%s: ", prompt)
|
|
|
|
text, err := reader.ReadString('\n')
|
|
|
|
text = strings.TrimSpace(text)
|
|
|
|
|
|
|
|
if required && len(text) == 0 {
|
|
|
|
return "", errors.New("Required string is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return text, err
|
|
|
|
}
|
|
|
|
|
2018-03-10 05:23:05 +08:00
|
|
|
func main() {
|
|
|
|
if err := RootCmd.Execute(); err != nil {
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
}
|