-
Notifications
You must be signed in to change notification settings - Fork 14
/
scriptrearrange.go
154 lines (148 loc) · 4.33 KB
/
scriptrearrange.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
// Package main (scriptrearrange.go) :
// These methods are for rearranging scripts in a project.
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/briandowns/spinner"
rearrange "github.com/tanaikech/go-rearrange"
)
// rearrangeByTerminal : Rearranging scripts in a project using go-rearrange.
func (e *ExecutionContainer) rearrangeByTerminal() *ExecutionContainer {
baseProject := *e.Project
var scripts []string
for _, f := range e.Project.Files {
scripts = append(scripts, f.Name)
}
changedIndx, _, err := rearrange.Do(scripts, 3, false, true)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
var input string
fmt.Printf("## Please be careful.\n")
fmt.Printf("## When the script is rearranged, the revision of script is reset once.\n")
fmt.Printf("Reflect the rearranged result? [y or n] ... ")
if _, err := fmt.Scan(&input); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
if input == "y" {
s := spinner.New([]string{"/", "|", "\\", "|"}, 100*time.Millisecond)
s.UpdateSpeed(200 * time.Millisecond)
fmt.Printf("Please wait a moment...")
s.Start()
e.rearrange(baseProject, changedIndx)
s.Stop()
fmt.Printf("\n")
return e
}
e.Msg = append(e.Msg, "Scripts of project were NOT rearranged.")
return e
}
// rearrange : Rearranging scripts in a project using a configuration file.
func (e *ExecutionContainer) rearrangeByFile(data []string) *ExecutionContainer {
baseProject := *e.Project
var temp []string
dupChk := map[string]bool{}
for _, e := range data {
if !dupChk[e] {
dupChk[e] = true
temp = append(temp, e)
}
}
if len(temp) == len(data) {
if len(e.Project.Files) == len(data) {
cn := 0
for i, e := range e.Project.Files {
if e.Name == data[i] {
cn += 1
}
}
if cn != len(e.Project.Files) {
cn = 0
var changedIndx []string
for _, f := range data {
for i, g := range e.Project.Files {
if g.Name == f {
cn += 1
changedIndx = append(changedIndx, strconv.Itoa(i))
}
}
}
if cn == len(e.Project.Files) {
e.rearrange(baseProject, changedIndx)
return e
}
e.Msg = append(e.Msg, "Error: Script names of inputted file are different for script names in project.")
return e
}
e.Msg = append(e.Msg, "Error: Order of inputted file are the same to the order in project.")
return e
}
e.Msg = append(e.Msg, "Error: Number of script names of inputted file are different for number of scripts in project.")
return e
}
e.Msg = append(e.Msg, "Error: There are duplicated names in script names of inputted file.")
return e
}
// rearrange : Main method for rearranging scripts.
func (e *ExecutionContainer) rearrange(baseProject Project, changedIndx []string) {
var temp1 Project
const layout = "20060102_150405_"
t := time.Now()
temp1.Files = append(temp1.Files, File{
Name: "Dummy_" + t.Format(layout) + t.AddDate(0, 0, 2).Weekday().String(),
Source: "// This is a dummy.",
Type: "SERVER_JS",
})
temp1.Files = append(temp1.Files, File{
Name: "appsscript",
Source: "{}",
Type: "JSON",
})
e.Project = &temp1
e.projectUpdate2()
var temp2 Project
for _, e := range changedIndx {
idx, _ := strconv.Atoi(e)
temp2.Files = append(temp2.Files, baseProject.Files[idx])
}
e.Project = &temp2
e.projectUpdate2()
var from, to []string
for i, f := range e.Project.Files {
from = append(from, baseProject.Files[i].Name)
to = append(to, f.Name)
}
msg := fmt.Sprintf("Scripts in project were rearranged from [%s] to [%s].", strings.Join(from, ", "), strings.Join(to, ", "))
e.Msg = []string{msg}
}
// getRearrangeTemplate : Retrieve data from template file for rearranging.
func getRearrangeTemplate(templateFile string) []string {
var data []string
f, err := os.Open(templateFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Script '%s' is not found.\n", templateFile)
os.Exit(1)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if scanner.Text() == "end" {
break
}
if scanner.Text() != "" {
data = append(data, scanner.Text())
}
}
if scanner.Err() != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", scanner.Err())
os.Exit(1)
}
return data
}