/
edenUtils.go
107 lines (100 loc) · 3.08 KB
/
edenUtils.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
package cmd
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/lf-edge/eden/pkg/defaults"
"github.com/lf-edge/eden/pkg/eden"
"github.com/lf-edge/eden/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
syslogOutput string
eveReleaseOutput string
)
var utilsCmd = &cobra.Command{
Use: "utils",
Short: "Eden utilities",
Long: `Additional utilities for EDEN.`,
}
var sdInfoEveCmd = &cobra.Command{
Use: "sd <SD_DEVICE_PATH>",
Short: "get info from SD card",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
eveInfo, err := eden.GetInfoFromSDCard(args[0])
if err != nil {
log.Info("Check is EVE on SD and your access to read SD")
log.Fatalf("Problem with access to EVE partitions: %v", err)
}
if eveInfo.EVERelease == nil {
log.Warning("No eve-release found. Probably, no EVE on SD card")
} else {
if err = ioutil.WriteFile(eveReleaseOutput, eveInfo.EVERelease, 0666); err != nil {
log.Fatal(err)
}
log.Infof("Your eve-release in %s", eveReleaseOutput)
}
if eveInfo.Syslog == nil {
log.Warning("No syslog found, EVE may not have started yet")
} else {
if err = ioutil.WriteFile(syslogOutput, eveInfo.Syslog, 0666); err != nil {
log.Fatal(err)
}
log.Infof("Your syslog in %s", syslogOutput)
}
},
}
var uploadGitCmd = &cobra.Command{
Use: "gitupload <file or directory> " +
"<git repo in notation https://GIT_LOGIN:GIT_TOKEN@GIT_REPO> <branch> [directory in git]",
Long: "Upload file or directory to provided git branch into directory with the same name as branch " +
"or into provided directory",
Args: cobra.RangeArgs(3, 4),
Run: func(cmd *cobra.Command, args []string) {
if _, err := os.Stat(args[0]); os.IsNotExist(err) {
log.Fatal(err)
}
absPath, err := filepath.Abs(args[0])
if err != nil {
log.Fatal(err)
}
image := fmt.Sprintf("%s:%s", defaults.DefaultProcContainerRef, defaults.DefaultProcTag)
directoryToSaveOnGit := args[2]
if len(args) == 4 {
directoryToSaveOnGit = args[3]
}
commandToRun := fmt.Sprintf("-i /in/%s -o %s -b %s -d %s git",
filepath.Base(absPath), args[1], args[2], directoryToSaveOnGit)
volumeMap := map[string]string{"/in": filepath.Dir(absPath)}
var result string
if result, err = utils.RunDockerCommand(image, commandToRun, volumeMap); err != nil {
log.Fatal(err)
}
fmt.Println(result)
},
}
func utilsInit() {
currentPath, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
utilsCmd.AddCommand(completionCmd)
utilsCmd.AddCommand(templateCmd)
utilsCmd.AddCommand(downloaderCmd)
downloaderInit()
utilsCmd.AddCommand(ociImageCmd)
ociImageInit()
utilsCmd.AddCommand(certsCmd)
certsInit()
utilsCmd.AddCommand(gcpCmd)
gcpInit()
utilsCmd.AddCommand(sdInfoEveCmd)
debugInit()
utilsCmd.AddCommand(debugCmd)
utilsCmd.AddCommand(uploadGitCmd)
sdInfoEveCmd.Flags().StringVar(&syslogOutput, "syslog-out", filepath.Join(currentPath, "syslog.txt"), "File to save syslog.txt")
sdInfoEveCmd.Flags().StringVar(&eveReleaseOutput, "everelease-out", filepath.Join(currentPath, "eve-release"), "File to save eve-release")
}