-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
88 lines (76 loc) · 2.48 KB
/
options.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
// Package core is a collection of functions missing from Go's standard library
package core
import (
"path/filepath"
"time"
"github.com/drgo/core/files"
)
const defaultDebugLevel = 1
//RunOptions holds info related to a specific app run
type RunOptions struct {
Debug int
ConfigFileName string //if not empty, points to source config file that was used to load the config
Command string
InputFileNames []string
OverWriteOutputFile bool
OutputFileName string
OutputFormat string
WorkDirName string
InputDirName string
InputFilesGlob string
OutputDirName string
TempDirName string
PreserveWorkFiles bool
DefaultConfigFileName string
ExecutableVersion string `mdson:"-"`
LibVersion string `mdson:"-"`
ExecutableName string `mdson:"-"`
DefaultScriptFileName string `mdson:"-"`
Serve bool `mdson:"-"`
Watch bool `mdson:"-"`
}
//DefaultOptions returns a default option setting
func DefaultOptions() *RunOptions {
return &RunOptions{
Debug: defaultDebugLevel,
}
}
//NewOptions returns a new option setting
func NewOptions(ConfigFileName string, debug int) *RunOptions {
return &RunOptions{
ConfigFileName: ConfigFileName,
Debug: debug,
}
}
//SetDebug sets the debug level
func (opt *RunOptions) SetDebug(level int) *RunOptions {
opt.Debug = level
return opt
}
// GetTempFileName returns potentially non-unique name for a temp file
func (opt *RunOptions) GetTempFileName(prefix, postfix string) string {
if opt.TempDirName == "" {
opt.TempDirName, _ = files.GetTempDir()
}
return filepath.Clean(filepath.Join(opt.TempDirName, prefix+opt.ExecutableName+time.Now().Format("20060102150405")+postfix))
}
//TODO: return a writercloser
// func (opt *RunOptions) GetTempFile(string) string {
// if opt.TempDirName == "" {
// opt.TempDirName, _ = files.GetTempDir()
// }
// return filepath.Clean(filepath.Join(dir, prefix+opt.ExecutableName+time.Now().Format("20060102150405")+postfix))
// }
// TODO: clean up following code
//SaveToMDSon saves job configuration to specified MDSon writer
// func (opt *RunOptions) SaveToMDSon(w io.Writer) error {
// buf, err := mdson.Marshal(opt)
// if err != nil {
// return fmt.Errorf("failed to save job configuration: %s", err)
// }
// _, err = w.Write(buf)
// if err != nil {
// return fmt.Errorf("failed to save job configuration: %s", err)
// }
// return nil
// }