-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathutil.go
103 lines (83 loc) · 2.28 KB
/
util.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
package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/project-flogo/cli/common"
"github.com/project-flogo/core/app"
)
func readAppDescriptor(project common.AppProject) (*app.Config, error) {
appDescriptorFile, err := os.Open(filepath.Join(project.Dir(), fileFlogoJson))
if err != nil {
return nil, err
}
defer appDescriptorFile.Close()
appDescriptorData, err := ioutil.ReadAll(appDescriptorFile)
if err != nil {
return nil, err
}
var appDescriptor app.Config
err = json.Unmarshal([]byte(appDescriptorData), &appDescriptor)
if err != nil {
return nil, err
}
return &appDescriptor, nil
}
func writeAppDescriptor(project common.AppProject, appDescriptor *app.Config) error {
appDescriptorUpdated, err := json.MarshalIndent(appDescriptor, "", " ")
if err != nil {
return err
}
appDescriptorUpdatedJson := string(appDescriptorUpdated)
err = ioutil.WriteFile(filepath.Join(project.Dir(), fileFlogoJson), []byte(appDescriptorUpdatedJson), 0644)
if err != nil {
return err
}
return nil
}
func backupMain(project common.AppProject) error {
mainGo := filepath.Join(project.SrcDir(), fileMainGo)
mainGoBak := filepath.Join(project.SrcDir(), fileMainGo+".bak")
if _, err := os.Stat(mainGo); err == nil {
//main found, check for backup main in case we have to remove it
if _, err := os.Stat(mainGoBak); err == nil {
//remove old main backup
if Verbose() {
fmt.Printf("Removing old main backup: %s\n", mainGoBak)
}
err = os.Rename(mainGoBak, mainGo)
if err != nil {
return err
}
}
if Verbose() {
fmt.Println("Backing up main.go")
}
err = os.Rename(mainGo, mainGoBak)
if err != nil {
return err
}
}
return nil
}
func restoreMain(project common.AppProject) error {
mainGo := filepath.Join(project.SrcDir(), fileMainGo)
mainGoBak := filepath.Join(project.SrcDir(), fileMainGo+".bak")
if _, err := os.Stat(mainGo); err != nil {
//main not found, check for backup main
if _, err := os.Stat(mainGoBak); err == nil {
if Verbose() {
fmt.Printf("Restoring main from: %s\n", mainGoBak)
}
err = os.Rename(mainGoBak, mainGo)
if err != nil {
return err
}
} else if _, err := os.Stat(mainGo); err != nil {
return fmt.Errorf("project corrupt, main missing")
}
}
return nil
}