2022-09-25 22:04:44 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2023-10-24 00:12:48 +03:00
|
|
|
"encoding/json"
|
2022-09-25 22:04:44 +03:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/DataDrake/cli-ng/v2/cmd"
|
|
|
|
"github.com/hyprspace/hyprspace/config"
|
|
|
|
"github.com/libp2p/go-libp2p"
|
2022-11-11 21:54:57 +02:00
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
2022-09-26 01:00:05 +03:00
|
|
|
"github.com/multiformats/go-multibase"
|
2022-09-25 22:04:44 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Init creates a configuration for a Hyprspace Interface.
|
|
|
|
var Init = cmd.Sub{
|
|
|
|
Name: "init",
|
|
|
|
Alias: "i",
|
|
|
|
Short: "Initialize An Interface Config",
|
|
|
|
Args: &InitArgs{},
|
|
|
|
Run: InitRun,
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitArgs handles the specific arguments for the init command.
|
|
|
|
type InitArgs struct {
|
|
|
|
InterfaceName string
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitRun handles the execution of the init command.
|
|
|
|
func InitRun(r *cmd.Root, c *cmd.Sub) {
|
|
|
|
// Parse Command Arguments
|
|
|
|
args := c.Args.(*InitArgs)
|
|
|
|
|
|
|
|
// Parse Global Config Flag
|
|
|
|
configPath := r.Flags.(*GlobalFlags).Config
|
|
|
|
if configPath == "" {
|
2023-10-24 00:12:48 +03:00
|
|
|
configPath = "/etc/hyprspace/" + args.InterfaceName + ".json"
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create New Libp2p Node
|
|
|
|
host, err := libp2p.New()
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
// Get Node's Private Key
|
|
|
|
keyBytes, err := crypto.MarshalPrivateKey(host.Peerstore().PrivKey(host.ID()))
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
// Setup an initial default command.
|
|
|
|
new := config.Config{
|
|
|
|
Interface: config.Interface{
|
|
|
|
Name: args.InterfaceName,
|
|
|
|
ListenPort: 8001,
|
|
|
|
Address: "10.1.1.1/24",
|
2023-01-23 01:34:17 +02:00
|
|
|
ID: host.ID(),
|
2022-09-26 01:00:05 +03:00
|
|
|
PrivateKey: multibase.MustNewEncoder(multibase.Base58BTC).Encode(keyBytes),
|
2022-09-25 22:04:44 +03:00
|
|
|
},
|
2023-10-24 00:12:48 +03:00
|
|
|
Peers: make([]config.Peer, 0),
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
|
|
|
|
2023-10-24 00:12:48 +03:00
|
|
|
out, err := json.MarshalIndent(&new, "", " ")
|
2022-09-25 22:04:44 +03:00
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
err = os.MkdirAll(filepath.Dir(configPath), os.ModePerm)
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
f, err := os.Create(configPath)
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
// Write out config to file.
|
|
|
|
_, err = f.Write(out)
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
// Print config creation message to user
|
|
|
|
fmt.Printf("Initialized new config at %s\n", configPath)
|
|
|
|
fmt.Println("To edit the config run,")
|
|
|
|
fmt.Println()
|
|
|
|
if strings.HasPrefix(configPath, "/etc/") {
|
|
|
|
fmt.Printf(" sudo nano %s\n", configPath)
|
|
|
|
} else {
|
|
|
|
fmt.Printf(" nano %s\n", configPath)
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
}
|