This repository has been archived by the owner on Jun 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
169 lines (147 loc) · 4.95 KB
/
add.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Copyright © 2020 Will Rowe <w.p.m.rowe@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd
import (
"fmt"
"os"
ipfs "github.com/ipfs/go-ipfs-api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/will-rowe/scribe/src/backend"
"github.com/will-rowe/scribe/src/config"
"github.com/will-rowe/scribe/src/records"
)
// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add <run|library>",
Short: "Add a run or library to an existing project",
Long: `Add a run or library to an existing project.
This will collect the project (registered using scribe set --project XXX) and then add
specified record to the project, before committing it back to the IPFS.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
runAdd(args[0])
},
}
func init() {
rootCmd.AddCommand(addCmd)
}
// runAdd is the main block for the add subcommand
func runAdd(arg string) {
// check the arg is a known record type
switch arg {
case "run", "library":
break
default:
fmt.Printf("unrecognised argument (%v), use either run|library\n", arg)
os.Exit(1)
}
// run the config checker to make sure we've got everything
if err := config.CheckConfig(); err != nil {
fmt.Println(err)
os.Exit(1)
}
// start the subcommand
log.Info("------------SCRIBE------------")
log.Info("starting the add subcommand...")
log.Infof("\tconfig file: %v", viper.ConfigFileUsed())
// configure the daemon
log.Info("configuring IPFS daemon...")
config, err := config.DumpConfig2Mem()
if err != nil {
log.Fatal(err)
}
if err := backend.ConfigureDaemon(config); err != nil {
log.Fatal(err)
}
log.Infof("\tupdated IPFS daemon using the scribe config")
// check if the IPFS daemon is running (launch if not)
tmpShell := ipfs.NewShell(backend.GetAPI())
if !tmpShell.IsUp() {
log.Infof("\tlaunching daemon...")
if err := backend.LaunchDaemon(config); err != nil {
log.Fatal(err)
}
}
log.Infof("\tdaemon is running")
// init the node
log.Info("initialising the node...")
node, err := backend.InitNode(backend.GetAPI())
if err != nil {
log.Fatal(err)
}
nodeIdentity, err := node.Identity()
if err != nil {
log.Fatal(err)
}
log.Infof("\tAPI server listening on: %s", backend.GetAPI())
log.Infof("\tnode identity: %v", nodeIdentity.ID)
node.SetProject(config.Project)
log.Infof("\tregistered node with proejct: %v", node.GetProject())
// init the local project database
log.Info("initialising the local project database...")
db := records.InitDB()
if len(config.RemoteCID) != 0 {
log.Infof("\tremote CID found: %s", config.RemoteCID)
log.Info("\tpulling project database from IPFS...")
if err := db.Pull(node, config.RemoteCID); err != nil {
log.Fatal(err)
}
log.Infof("\tnumber of projects added to local database: %d", db.GetNumProjects())
} else {
log.Info("\tno existing CID found")
}
// check for the required project
log.Info("checking the local project database...")
proj, err := db.GetProject(config.Project)
switch err {
case nil:
log.Infof("\tproject found for %v", config.Project)
case records.ErrNotFound:
log.Infof("\tproject not found for %v", config.Project)
log.Info("\tcreating project...")
proj = records.InitProject(config.Project)
log.Info("\tadding to the database...")
if err := db.AddProject(proj); err != nil {
log.Fatal(err)
}
log.Info("\tpushing database changes to IPFS...")
cid, err := db.Push(node)
if err != nil {
log.Fatal(err)
}
log.Info("\tupdating CID...")
config.RemoteCID = cid
viper.Set("remoteCID", cid)
if err := viper.WriteConfig(); err != nil {
log.Fatal(err)
}
log.Infof("\tCID updated: %s", config.RemoteCID)
log.Infof("\tview on: %v", fmt.Sprintf("https://explore.ipld.io/#/explore/%s", config.RemoteCID))
default:
log.Fatal(err)
}
// work with the project
log.Infof("\tproject loaded: %v", proj.GetLabel())
// test message
if err := node.Publish("just loaded the project over here..."); err != nil {
log.Fatal(err)
}
}