/
add-graph.go
89 lines (80 loc) · 1.98 KB
/
add-graph.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
package cli
import (
"fmt"
"io/ioutil"
"log"
"os"
"github.com/QMSTR/qmstr/lib/go-qmstr/service"
"github.com/spf13/cobra"
"golang.org/x/net/context"
yaml "gopkg.in/yaml.v2"
)
type missingPieces struct {
File []*service.FileNode
}
var addGraphCmd = &cobra.Command{
Use: "add-graph [config_file]",
Short: "Add partial graphs from a configuration file",
Long: `Add missing files nodes and the connection between them.
Provide the extra information you want to include in the database,
through a configuration yaml file.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
setUpBuildService()
if err := addMissingPieces(args); err != nil {
Log.Fatalf("add-graph failed: %v", err)
}
tearDownServer()
},
}
func init() {
rootCmd.AddCommand(addGraphCmd)
}
func addMissingPieces(args []string) error {
configFile := args[0]
mp := &missingPieces{}
if err := mp.readConfigFile(configFile); err != nil {
return fmt.Errorf("Failed to read configuration from %s: %v", configFile, err)
}
stream, err := buildServiceClient.Build(context.Background())
defer func() {
res, err := stream.CloseAndRecv()
if err != nil {
log.Fatalf("Failed to close the filenode stream: %v", err)
}
if !res.Success {
log.Fatalln("Server filenode stream failed")
}
}()
if err != nil {
return err
}
for _, fnode := range mp.File {
if err := stream.Send(fnode); err != nil {
log.Fatalf("Failed to send filenode to server")
}
log.Printf("added filenode %s", fnode.Path)
}
return nil
}
func (mp *missingPieces) readConfigFile(configFile string) error {
if _, err := os.Stat(configFile); os.IsNotExist(err) {
return fmt.Errorf("File %s not found", configFile)
}
cFile, err := os.Open(configFile)
if err != nil {
return err
}
data, err := ioutil.ReadAll(cFile)
if err != nil {
return err
}
err = yaml.Unmarshal(data, mp)
if err != nil {
return err
}
if mp.File == nil {
return fmt.Errorf("no data found -- check indentation")
}
return nil
}