-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidy.go
150 lines (124 loc) · 3.3 KB
/
tidy.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
"gopkg.in/gcfg.v1"
)
const RFC1123 = "2006/01/02 15:04:05 MST"
const IniFile = "tidy.ini"
type config struct {
Path struct {
Workpath string
Rulepath string
}
}
//規則格納用
type rule struct {
Match string
Replace string
Moveto string
}
func main() {
IniFilePath, WorkPath, RulePath, err := initPath()
if "" != err {
fmt.Println(err)
pause()
os.Exit(0)
}
fmt.Println(IniFilePath)
fmt.Println(WorkPath)
fmt.Println(RulePath)
pause()
}
func createDefaultRuleFileName() string {
t := strings.Split(time.Now().Format(RFC1123), "/")
year := t[0]
quarter, _ := strconv.Atoi(t[1])
quarter = (quarter-1)/3 + 1
return (year + "Q" + strconv.Itoa(quarter) + ".txt")
}
func checkArgs() bool {
return true
}
func showUsage() {
fmt.Println("Usage : tidy.exe [ini_File's_Path]")
}
func pause() {
fmt.Print("Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
func initPath() (string, string, string, string) {
//var IniFilePath string
var PathError string
var ErrorMessage string
IniFilePath, err := getIniFile()
if false == err {
ErrorMessage += "ini file does not exist. \n"
return IniFilePath, "", "", ErrorMessage
}
//iniファイル読み取り
WorkPath, RulePath, PathError := readIniFile(IniFilePath)
if "" != PathError {
ErrorMessage += PathError
return IniFilePath, "", "", ErrorMessage
}
return IniFilePath, WorkPath, RulePath, ErrorMessage
}
// ini ファイルのpath 取得と存在チェック
// 引数でini ファイルのパスが指定されていない場合は、実行ディレクトリ内をチェック
func getIniFile() (string, bool) {
var IniFilePath string
if len(os.Args) == 1 {
ExecPath, _ := os.Getwd()
IniFilePath = ExecPath + "\\" + IniFile
} else {
IniFilePath = os.Args[1]
}
_, err := os.Stat(IniFilePath)
return IniFilePath, !os.IsNotExist(err)
}
// ini ファイルの内容読み取り
func readIniFile(IniFilePath string) (string, string, string) {
var cfg config
var ErrorMessage string
var RulePath string
err := gcfg.ReadFileInto(&cfg, IniFilePath)
if err != nil {
log.Fatalf("Failed to parse config file: %s", err)
}
//必須項目 workpath のチェック
if "" == cfg.Path.Workpath {
ErrorMessage += "No setting for Path to Work Folder.\n"
}
//ワークフォルダ存在チェックとディレクトリ判定
WorkPath := cfg.Path.Workpath
WorkPathInfo, err := os.Stat(WorkPath)
if false == !os.IsNotExist(err) {
ErrorMessage += "Path '" + WorkPath + "' does not exist.\n"
} else if false == WorkPathInfo.IsDir() {
ErrorMessage += "Path '" + WorkPath + "' is not a directory.\n"
}
//Rulepath 設定値のフォルダ判定
// フォルダあるいは設定無しだった場合のデフォルトファイル名でのパス設定
RulePath = cfg.Path.Rulepath
if "" != RulePath {
PathInfo, _ := os.Stat(RulePath)
if true == PathInfo.IsDir() {
RulePath += "\\" + createDefaultRuleFileName()
}
} else {
ExecPath, _ := os.Getwd()
RulePath += ExecPath + "\\" + createDefaultRuleFileName()
}
// 規則ファイルの存在チェック
_, err = os.Stat(RulePath)
if false == !os.IsNotExist(err) {
ErrorMessage += "Rule File does not exist. -> " + RulePath + "\n"
}
return WorkPath, RulePath, ErrorMessage
}