-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (137 loc) · 4.14 KB
/
main.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
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"database/sql"
"github.com/ilyakaznacheev/cleanenv"
shell "github.com/ipfs/go-ipfs-api"
_ "github.com/mattn/go-sqlite3" // Import go-sqlite3 library
"github.com/thatisuday/commando"
)
// config file construct
// Port and Host are used for the local or remote IPFS daemon
// Database ist the file name of the sqlite database
type Config struct {
Port string `yaml:"port" env:"PORT" env-default:"5001"`
Host string `yaml:"host" env:"HOST" env-default:"localhost"`
Database string `yaml:"db" env:"DB" env-default:"ipfs-tag.db"`
}
var verbose bool = false
func main() {
var add string
var cfg Config
commando.
SetExecutableName("ipfs-tag").
SetVersion("v0.0.1").
SetDescription("IPFS-tag supplies a tag layer on top of IPFS")
// main command
commando.
Register(nil).
AddArgument("add", "add files to IPFS store", ""). // required
AddFlag("verbose,V", "display log information ", commando.Bool, false). // optional
SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
add = args["add"].Value
verbose, _ = flags["verbose"].GetBool()
})
commando.Parse(nil)
// load config
err := cleanenv.ReadConfig("ipfs-tag.yml", &cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading configuration file: %s", err)
}
// check for db file and create it, if it doesn't exist
if _, err := os.Stat(cfg.Database); os.IsNotExist(err) {
if verbose {
log.Println("Database does not exist yet.")
}
createDataBase(cfg.Database)
sqliteDatabase, _ := sql.Open("sqlite3", cfg.Database) // Open the created SQLite File
defer sqliteDatabase.Close() // Defer Closing the database
createTable(sqliteDatabase)
}
// open db for the main app
sqliteDatabase, _ := sql.Open("sqlite3", cfg.Database) // Open the created SQLite File
defer sqliteDatabase.Close() // Defer Closing the database
// add files
// check, if the file exist
file, err := os.Open(add)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading file: %s", err)
}
// add the file and return the cid
cid, err := addIPFSFile(cfg, file)
if err != nil {
log.Printf("Error adding String to IPFS: %s", err)
}
// insert cid into db
insertFile(sqliteDatabase, cid, "teststring", add)
}
// create SQLite db file
func createDataBase(dbFileName string) {
if verbose {
log.Println("Creating sqlite-database.db...")
}
file, err := os.Create(dbFileName) // Create SQLite file
if err != nil {
log.Fatal(err.Error())
}
file.Close()
if verbose {
log.Println("database created")
}
}
// returns the CID and the possible error
func addIPFSFile(cfg Config, file io.Reader) (string, error) {
// Where your local node is running on localhost:5001
sh := shell.NewShell(cfg.Host + ":" + cfg.Port)
if !sh.IsUp() {
log.Println("Gateway is not up")
return "", errors.New("gateway not up")
}
//cid, err := sh.Add(strings.NewReader("hello world!"))
cid, err := sh.Add(file)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
return "", errors.New("cannot add hash")
}
log.Printf("added %s", cid)
return cid, nil
}
// create tables in database
func createTable(db *sql.DB) {
createFileTableSQL := `CREATE TABLE files (
"idFile" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"hash" TEXT,
"name" TEXT,
"path" TEXT
);` // SQL Statement for Create Table
if verbose {
log.Println("Create file table...")
}
statement, err := db.Prepare(createFileTableSQL) // Prepare SQL Statement
if err != nil {
log.Fatal(err.Error())
}
statement.Exec() // Execute SQL Statements
if verbose {
log.Println("File table created")
}
}
// insert the hash to the sqlite db
func insertFile(db *sql.DB, hash string, name string, path string) {
if verbose {
log.Println("Inserting file record for " + path)
}
insertFileSQL := `INSERT INTO files(hash, name, path) VALUES (?, ?, ?)`
statement, err := db.Prepare(insertFileSQL) // Prepare statement. This is good to avoid SQL injections
if err != nil {
log.Fatalln(err.Error())
}
_, err = statement.Exec(hash, name, path)
if err != nil {
log.Fatalln(err.Error())
}
}