forked from garetht/amanar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_file.go
154 lines (133 loc) · 3.96 KB
/
shell_file.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 amanar
import (
"github.com/mvdan/sh/syntax"
"os"
"fmt"
)
// Example AST format
//0: *syntax.Stmt {
//. . . . Comments: []syntax.Comment (len = 0) {}
//. . . . Cmd: *syntax.DeclClause {
//. . . . . Variant: *syntax.Lit {
//. . . . . . ValuePos: 1:1
//. . . . . . ValueEnd: 1:7
//. . . . . . Value: "export"
//. . . . . }
//. . . . . Opts: []*syntax.Word (len = 0) {}
//. . . . . Assigns: []*syntax.Assign (len = 1) {
//. . . . . . 0: *syntax.Assign {
//. . . . . . . Append: false
//. . . . . . . Naked: false
//. . . . . . . Name: *syntax.Lit {
//. . . . . . . . ValuePos: 1:8
//. . . . . . . . ValueEnd: 1:27
//. . . . . . . . Value: "TZANALYTIC_USERNAME"
//. . . . . . . }
//. . . . . . . Index: nil
//. . . . . . . Value: *syntax.Word {
//. . . . . . . . Parts: []syntax.WordPart (len = 1) {
//. . . . . . . . . 0: *syntax.Lit {
//. . . . . . . . . . ValuePos: 1:28
//. . . . . . . . . . ValueEnd: 1:44
//. . . . . . . . . . Value: "v-read-0qy0pw25x"
//. . . . . . . . . }
//. . . . . . . . }
//. . . . . . . }
//. . . . . . . Array: nil
//. . . . . . }
//. . . . . }
//. . . . }
//. . . . Position: 1:1
//. . . . Semicolon: 0:0
//. . . . Negated: false
//. . . . Background: false
//. . . . Coprocess: false
//. . . . Redirs: []*syntax.Redirect (len = 0) {}
//. . . }
func createLit(value string) *syntax.Lit {
return &syntax.Lit{
Value: value,
}
}
func createExportStatement(variableName, assignedValue string) *syntax.Stmt {
return &syntax.Stmt{
Cmd: &syntax.DeclClause{
Variant: createLit("export"),
Assigns: []*syntax.Assign{
{
Name: createLit(variableName),
Value: createWord(assignedValue),
},
},
},
}
}
func createWord(value string) *syntax.Word {
return &syntax.Word{
Parts: []syntax.WordPart{createLit(value)},
}
}
type ShellFile struct {
AST *syntax.File
filepath string
}
func (sf *ShellFile) UpdateCredentials(usernameVariable, passwordVariable string, credentials *Credentials) {
usernameUpdated := false
passwordUpdated := false
// Walk the tree to update existing username and password variables
syntax.Walk(sf.AST, func(node syntax.Node) bool {
switch x := node.(type) {
case *syntax.DeclClause:
if x.Variant.Value == "export" && len(x.Assigns) == 1 {
if x.Assigns[0].Name.Value == usernameVariable {
x.Assigns[0].Value = createWord(credentials.Username)
usernameUpdated = true
}
if x.Assigns[0].Name.Value == passwordVariable {
x.Assigns[0].Value = createWord(credentials.Password)
passwordUpdated = true
}
return false
}
}
return true
})
// Create additional environment variables at the end of the file if
// they do not already exist
if !usernameUpdated {
sf.AST.Stmts = append(sf.AST.Stmts, createExportStatement(usernameVariable, credentials.Username))
}
if !passwordUpdated {
sf.AST.Stmts = append(sf.AST.Stmts, createExportStatement(passwordVariable, credentials.Password))
}
}
func (sf *ShellFile) WriteToDisk() error {
file, err := os.OpenFile(sf.filepath, os.O_WRONLY|os.O_CREATE, 0644)
defer file.Close()
if err != nil {
return fmt.Errorf("could not open shell script '%s' for writing: %s", sf.filepath, err)
}
printer := syntax.NewPrinter()
err = printer.Print(file, sf.AST)
if err != nil {
return fmt.Errorf("could not write AST to filepath '%s': %s", sf.filepath, err)
}
return nil
}
func NewShellFile(filepath string) (*ShellFile, error) {
p := syntax.NewParser(syntax.KeepComments, syntax.Variant(syntax.LangBash))
file, err := os.OpenFile(filepath, os.O_RDONLY|os.O_CREATE, 0644)
defer file.Close()
if err != nil {
return nil, err
}
syntaxTree, err := p.Parse(file, filepath)
if err != nil {
return nil, err
}
shellFile := &ShellFile{
AST: syntaxTree,
filepath: filepath,
}
return shellFile, nil
}