-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from everFinance/feature/add-cli
feat(): add arseeding cli
- Loading branch information
Showing
13 changed files
with
394 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright © 2023 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/everFinance/arseeding/schema" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var cfgFile string | ||
var cfg schema.Config | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "arseeding", | ||
Short: "arseeding", | ||
Long: `arseeding`, | ||
Version: "v1.2.3", | ||
// Uncomment the following line if your bare application | ||
// has an action associated with it: | ||
// Run: func(cmd *cobra.Command, args []string) { }, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
|
||
// Here you will define your flags and configuration settings. | ||
// Cobra supports persistent flags, which, if defined here, | ||
// will be global for your application. | ||
|
||
rootCmd.PersistentFlags().StringVar(&cfgFile, "cfg", "", "cfg file (default is $./arseeding.yaml)") | ||
|
||
// Cobra also supports local flags, which will only run | ||
// when this action is called directly. | ||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} | ||
|
||
// initConfig reads in cfg file and ENV variables if set. | ||
func initConfig() { | ||
if cfgFile != "" { | ||
// Use cfg file from the flag. | ||
viper.SetConfigFile(cfgFile) | ||
} else { | ||
// Search cfg in home directory with name ".arseeding" (without extension). | ||
viper.AddConfigPath(".") | ||
viper.SetConfigType("yaml") | ||
viper.SetConfigName("arseeding") | ||
} | ||
|
||
viper.AutomaticEnv() // read in environment variables that match | ||
|
||
// If a cfg file is found, read it in. | ||
if err := viper.ReadInConfig(); err == nil { | ||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) | ||
} else { | ||
fmt.Println("can not find config file") | ||
panic(err) | ||
} | ||
|
||
if err := viper.Unmarshal(&cfg); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
Copyright © 2023 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/everFinance/arseeding" | ||
"github.com/everFinance/goar/types" | ||
"os" | ||
"os/exec" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
const pidFile string = ".arseeding_pid.lock" | ||
|
||
var daemon bool | ||
|
||
// startCmd represents the start command | ||
var startCmd = &cobra.Command{ | ||
Use: "start", | ||
Short: "start arseeding", | ||
Long: `start arseeding`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if daemon { | ||
if _, err := os.Stat(pidFile); err == nil { | ||
fmt.Println("Failed start, PID file exist.running...") | ||
return nil | ||
} | ||
|
||
path, err := os.Executable() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
command := exec.Command(path, "start") | ||
|
||
// add log | ||
logFileName := fmt.Sprintf("arseeding_%d.log", time.Now().Unix()) | ||
logFile, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
command.Stdout = logFile | ||
command.Stderr = logFile | ||
|
||
if err := command.Start(); err != nil { | ||
return err | ||
} | ||
err = os.WriteFile(pidFile, []byte(fmt.Sprintf("%d", command.Process.Pid)), 0666) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
daemon = false | ||
os.Exit(0) | ||
} else { | ||
runServer() | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(startCmd) | ||
|
||
startCmd.Flags().BoolVarP(&daemon, "deamon", "d", false, "is daemon?") | ||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// startCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// startCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} | ||
|
||
func runServer() { | ||
signals := make(chan os.Signal, 1) | ||
signal.Notify(signals, os.Interrupt, syscall.SIGTERM, os.Kill) | ||
|
||
useSqlite := false | ||
sqliteDir := "" | ||
|
||
tagJs := cfg.Tags | ||
if len(tagJs) == 0 { | ||
tagJs = `{"Community":"PermaDAO","Website":"permadao.com"}` | ||
} | ||
tagsMap := make(map[string]string) | ||
if err := json.Unmarshal([]byte(tagJs), &tagsMap); err != nil { | ||
panic(err) | ||
} | ||
|
||
customTags := make([]types.Tag, 0) | ||
for k, v := range tagsMap { | ||
customTags = append(customTags, types.Tag{ | ||
Name: k, | ||
Value: v, | ||
}) | ||
} | ||
|
||
m := arseeding.New(cfg.BoltDir, cfg.Mysql, sqliteDir, useSqlite, | ||
cfg.RollupKeyPath, cfg.ArNode, cfg.Pay, cfg.NoFee, cfg.Manifest, | ||
cfg.S3KV.UseS3, cfg.S3KV.AccKey, cfg.S3KV.SecretKey, cfg.S3KV.Prefix, cfg.S3KV.Region, cfg.S3KV.Endpoint, cfg.S3KV.User4Ever, | ||
cfg.AliyunKV.UseAliyun, cfg.AliyunKV.Endpoint, cfg.AliyunKV.AccKey, cfg.AliyunKV.SecretKey, cfg.AliyunKV.Prefix, | ||
cfg.MongoDBKV.UseMongoDB, cfg.MongoDBKV.Uri, | ||
cfg.Port, customTags, | ||
cfg.Kafka.Start, cfg.Kafka.Uri) | ||
|
||
m.Run(cfg.Port, cfg.BundleInterval) | ||
|
||
<-signals | ||
m.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
Copyright © 2023 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// stopCmd represents the stop command | ||
var stopCmd = &cobra.Command{ | ||
Use: "stop", | ||
Short: "stop arseeding", | ||
Long: `stop arseeding`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
strb, err := os.ReadFile(pidFile) | ||
if err != nil { | ||
fmt.Println("Stop server failed, err: %v", err) | ||
return nil | ||
} | ||
command := exec.Command("kill", string(strb)) | ||
if err := command.Start(); err != nil { | ||
return err | ||
} | ||
if err := os.Remove(pidFile); err != nil { | ||
return err | ||
} | ||
println("arseeding stopped") | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(stopCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// stopCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// stopCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
rollupKeyPath: ./data/arweave-keyfile-permaweb.json | ||
pay: https://api.everpay.io | ||
arNode: https://arweave.net | ||
mysql: arseeding:7YXMHxKeQmbeCpnD@(127.0.0.1:13306)/arseeding?charset=utf8mb4&parseTime=True&loc=Local | ||
port: :6080 | ||
manifest: true | ||
noFee: false | ||
bundleInterval: 120 | ||
boltDir: ./data | ||
s3KV: | ||
useS3: true | ||
user4Ever: false | ||
accKey: AKIAR63DMM2P4DGMWZU5 | ||
secretKey: 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 | ||
prefix: public-arseed | ||
region: ap-southeast-1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
Copyright © 2023 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package main | ||
|
||
import "github.com/everFinance/arseeding/cli/arseeding/cmd" | ||
|
||
func main() { | ||
cmd.Execute() | ||
} |
Oops, something went wrong.