Skip to content

Commit

Permalink
Merge pull request #181 from fairDataSociety/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
asabya authored Jan 26, 2022
2 parents 89f7d77 + 12eb5de commit ab15d5a
Show file tree
Hide file tree
Showing 23 changed files with 935 additions and 143 deletions.
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,50 @@ The user can share files in his pod with any other user just like in other centr
Pod creation is cheap. A user can create multiple pods and use it to organise his data. for ex: Personal-Pod, Applications-Pod etc.

### How to run FairOS-dfs?
Download the latest release from https://github.com/fairDataSociety/fairOS-dfs/releases.
Run the following command to download the latest release

```
curl -o- https://raw.githubusercontent.com/fairDataSociety/fairOS-dfs/master/download.sh | bash
```
```
wget -qO- https://raw.githubusercontent.com/fairDataSociety/fairOS-dfs/master/download.sh | bash
```

Or download the latest release from https://github.com/fairDataSociety/fairOS-dfs/releases.

Or use Docker to run the project https://docs.fairos.fairdatasociety.org/docs/fairOS-dfs/docker-installation.

Or build the latest version with the instruction https://docs.fairos.fairdatasociety.org/docs/fairOS-dfs/manual-installation.

### Configure FairOS-dfs
To get the most out of your FairOS-dfs it is important that you configure FairOS-dfs for your specific use case!

##### Configuration for Bee
```
bee:
bee-api-endpoint: http://localhost:1633
bee-debug-api-endpoint: http://localhost:1635
postage-batch-id: ""
```

##### Configuration for FairOS-dfs
```
dfs:
data-dir: /Users/fairos/.fairOS/dfs
ports:
http-port: :9090
pprof-port: :9091
```

#### Other configuration
```
cookie-domain: api.fairos.io
cors-allowed-origins: []
verbosity: trace
```

Run `dfs config` to see all configurations

### Introduction to Key Value Store over Swarm
[![](https://j.gifs.com/6XZwvl.gif)](https://gateway.ethswarm.org/access/130dcf7d01442836bc14c8c38db32ebfc4d5771c28677438b6a2a2a078bd1414)

Expand All @@ -53,3 +91,4 @@ https://docs.fairos.fairdatasociety.org/docs/fairOS-dfs/cli-reference
To make binaries for all platforms run this command

`./generate-exe.sh`

63 changes: 60 additions & 3 deletions cmd/dfs-cli/cmd/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,53 @@ func changeLivePrefix() (string, bool) {
return currentPrompt, true
}

var userSuggestions = []prompt.Suggest{
{Text: "new", Description: "create a new user"},
{Text: "del", Description: "delete a existing user"},
{Text: "login", Description: "login to a existing user"},
{Text: "logout", Description: "logout from a logged in user"},
{Text: "present", Description: "is user present"},
{Text: "export ", Description: "exports the user"},
{Text: "import ", Description: "imports the user"},
{Text: "stat ", Description: "shows information about a user"},
}

var podSuggestions = []prompt.Suggest{
{Text: "new", Description: "create a new pod for a user"},
{Text: "del", Description: "delete a existing pod of a user"},
{Text: "open", Description: "open to a existing pod of a user"},
{Text: "close", Description: "close a already opened pod of a user"},
{Text: "ls", Description: "list all the existing pods of a user"},
{Text: "stat", Description: "show the metadata of a pod of a user"},
{Text: "sync", Description: "sync the pod from swarm"},
}

var kvSuggestions = []prompt.Suggest{
{Text: "new", Description: "create new key value store"},
{Text: "delete", Description: "delete the key value store"},
{Text: "ls", Description: "lists all the key value stores"},
{Text: "open", Description: "open already created key value store"},
{Text: "get", Description: "get value from key"},
{Text: "put", Description: "put key and value in kv store"},
{Text: "del", Description: "delete key and value from the store"},
{Text: "loadcsv", Description: "loads the csv file in to kv store"},
{Text: "seek", Description: "seek to the given start prefix"},
{Text: "getnext", Description: "get the next element"},
}

var docSuggestions = []prompt.Suggest{
{Text: "new", Description: "creates a new document store"},
{Text: "delete", Description: "deletes a document store"},
{Text: "open", Description: "open the document store"},
{Text: "ls", Description: "list all document dbs"},
{Text: "count", Description: "count the docs in the table satisfying the expression"},
{Text: "find", Description: "find the docs in the table satisfying the expression and limit"},
{Text: "put", Description: "insert a json document in to document store"},
{Text: "get", Description: "get the document having the id from the store"},
{Text: "del", Description: "delete the document having the id from the store"},
{Text: "loadjson", Description: "load the json file in to the newly created document db"},
}

var suggestions = []prompt.Suggest{
{Text: "user new", Description: "create a new user"},
{Text: "user del", Description: "delete a existing user"},
Expand All @@ -149,7 +196,7 @@ var suggestions = []prompt.Suggest{
{Text: "pod del", Description: "delete a existing pod of a user"},
{Text: "pod open", Description: "open to a existing pod of a user"},
{Text: "pod close", Description: "close a already opened pod of a user"},
{Text: "pod ls", Description: "list all the existing pods of auser"},
{Text: "pod ls", Description: "list all the existing pods of a user"},
{Text: "pod stat", Description: "show the metadata of a pod of a user"},
{Text: "pod sync", Description: "sync the pod from swarm"},
{Text: "kv new", Description: "create new key value store"},
Expand Down Expand Up @@ -187,10 +234,20 @@ var suggestions = []prompt.Suggest{
}

func completer(in prompt.Document) []prompt.Suggest {
w := in.GetWordBeforeCursor()
if w == "" {
w := in.Text
if w == "" || len(strings.Split(w, " ")) >= 3 {
return []prompt.Suggest{}
}

if strings.HasPrefix(in.TextBeforeCursor(), "user") {
return prompt.FilterHasPrefix(userSuggestions, in.GetWordBeforeCursor(), true)
} else if strings.HasPrefix(in.TextBeforeCursor(), "pod") {
return prompt.FilterHasPrefix(podSuggestions, in.GetWordBeforeCursor(), true)
} else if strings.HasPrefix(in.TextBeforeCursor(), "kv") {
return prompt.FilterHasPrefix(kvSuggestions, in.GetWordBeforeCursor(), true)
} else if strings.HasPrefix(in.TextBeforeCursor(), "doc") {
return prompt.FilterHasPrefix(docSuggestions, in.GetWordBeforeCursor(), true)
}
return prompt.FilterHasPrefix(suggestions, w, true)
}

Expand Down
4 changes: 0 additions & 4 deletions cmd/dfs-cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ func init() {
defaultConfig := filepath.Join(home, ".fairOS/dfs-cli.yml")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", defaultConfig, "config file")

// 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")

rootCmd.PersistentFlags().String("fdfsHost", "127.0.0.1", "fdfs host")
rootCmd.PersistentFlags().String("fdfsPort", "9090", "fdfs port")

Expand Down
2 changes: 1 addition & 1 deletion cmd/dfs-cli/cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func presentUser(userName string) {
fmt.Println("user present: ", err)
return
}
var resp api.UserPresentResponse
var resp api.PresentResponse
err = json.Unmarshal(data, &resp)
if err != nil {
fmt.Println("import user: ", err)
Expand Down
49 changes: 49 additions & 0 deletions cmd/dfs/cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cmd

import (
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

var (
optionCORSAllowedOrigins = "cors-allowed-origins"
optionDFSDataDir = "dfs.data-dir"
optionDFSHttpPort = "dfs.ports.http-port"
optionDFSPprofPort = "dfs.ports.pprof-port"
optionVerbosity = "verbosity"
optionBeeApi = "bee.bee-api-endpoint"
optionBeeDebugApi = "bee.bee-debug-api-endpoint"
optionBeePostageBatchId = "bee.postage-batch-id"
optionCookieDomain = "cookie-domain"

defaultCORSAllowedOrigins = []string{}
defaultDFSHttpPort = ":9090"
defaultDFSPprofPort = ":9091"
defaultVerbosity = "trace"
defaultBeeApi = "http://localhost:1633"
defaultBeeDebugApi = "http://localhost:1635"
defaultCookieDomain = "api.fairos.io"
)

var configCmd = &cobra.Command{
Use: "config",
Short: "Print default or provided configuration in yaml format",
RunE: func(cmd *cobra.Command, args []string) (err error) {

if len(args) > 0 {
return cmd.Help()
}

d := config.AllSettings()
ym, err := yaml.Marshal(d)
if err != nil {
return err
}
cmd.Println(string(ym))
return nil
},
}

func init() {
rootCmd.AddCommand(configCmd)
}
118 changes: 87 additions & 31 deletions cmd/dfs/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,53 @@ import (
"os"
"path/filepath"

homedir "github.com/mitchellh/go-homedir"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
cfgFile string
beeHost string
beePort string
verbosity string
dataDir string
defaultDir = filepath.Join(".fairOS", "dfs")
defaultConfig = ".dfs.yaml"

cfgFile string
beeApi string
beeDebugApi string
verbosity string
dataDir string

dataDirPath string
config = viper.New()
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "dfs",
Short: "Decentralised file system over Swarm(https://ethswarm.org/)",
Long: `dfs is the file system layer of internetOS. It is a thin layer over Swarm.
It adds features to Swarm that is required by the internetOS to parallelize computation of data.
Long: `dfs is the file system layer of fairOS. It is a thin layer over Swarm.
It adds features to Swarm that is required by the fairOS to parallelize computation of data.
It manages the metadata of directories and files created and expose them to higher layers.
It can also be used as a standalone personal, decentralised drive over the internet`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := config.BindPFlag(optionDFSDataDir, cmd.Flags().Lookup("dataDir")); err != nil {
return err
}
if err := config.BindPFlag(optionBeeApi, cmd.Flags().Lookup("beeApi")); err != nil {
return err
}
if err := config.BindPFlag(optionBeeDebugApi, cmd.Flags().Lookup("beeDebugApi")); err != nil {
return err
}
if err := config.BindPFlag(optionVerbosity, cmd.Flags().Lookup("verbosity")); err != nil {
return err
}

//Run: func(cmd *cobra.Command, args []string) {
//},
dataDir = config.GetString(optionDFSDataDir)
beeApi = config.GetString(optionBeeApi)
beeDebugApi = config.GetString(optionBeeDebugApi)
verbosity = config.GetString(optionVerbosity)
return nil
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down Expand Up @@ -76,46 +99,79 @@ func init() {
fmt.Println(err)
os.Exit(1)
}

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
defaultConfig := filepath.Join(home, ".fairOS/dfs.yml")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", defaultConfig, "config file")

// 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")

defaultDataDir := filepath.Join(home, ".fairOS/dfs")
rootCmd.PersistentFlags().StringVar(&dataDir, "dataDir", defaultDataDir, "store data in this dir")
rootCmd.PersistentFlags().StringVar(&beeHost, "beeHost", "127.0.0.1", "bee host")
rootCmd.PersistentFlags().StringVar(&beePort, "beePort", "1633", "bee port")
rootCmd.PersistentFlags().StringVar(&verbosity, "verbosity", "5", "verbosity level")
configPath := filepath.Join(home, defaultConfig)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", configPath, "config file")

dataDirPath = filepath.Join(home, defaultDir)
rootCmd.PersistentFlags().String("dataDir", dataDirPath, "store data in this dir")
rootCmd.PersistentFlags().String("beeApi", "localhost:1633", "full bee api endpoint")
rootCmd.PersistentFlags().String("beeDebugApi", "localhost:1635", "full bee debug api endpoint")
rootCmd.PersistentFlags().String("verbosity", "trace", "verbosity level")

rootCmd.PersistentFlags().String("beeHost", "127.0.0.1", "bee host")
rootCmd.PersistentFlags().String("beePort", "1633", "bee port")
if err := rootCmd.PersistentFlags().MarkDeprecated("beeHost", "run --beeApi, full bee api endpoint"); err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := rootCmd.PersistentFlags().MarkDeprecated("beePort", "run --beeApi, full bee api endpoint"); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// check file stat
if _, err := os.Stat(cfgFile); err != nil {
// if there is no configFile, write it
writeConfig()
}
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
config.SetConfigFile(cfgFile)
} else {
// Find home dir.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// check file stat
cfgFile = filepath.Join(home, defaultConfig)
if _, err := os.Stat(cfgFile); err != nil {
// if there is no configFile, write it
writeConfig()
}

// Search config in home dir with name ".dfs" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".fairOS/dfs")
config.SetConfigFile(cfgFile)
}

viper.AutomaticEnv() // read in environment variables that match

config.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
if err := config.ReadInConfig(); err == nil {
fmt.Println("Using config file:", config.ConfigFileUsed())
}
}

func writeConfig() {
c := viper.New()
c.Set(optionCORSAllowedOrigins, defaultCORSAllowedOrigins)
c.Set(optionDFSDataDir, dataDirPath)
c.Set(optionDFSHttpPort, defaultDFSHttpPort)
c.Set(optionDFSPprofPort, defaultDFSPprofPort)
c.Set(optionVerbosity, defaultVerbosity)
c.Set(optionBeeApi, defaultBeeApi)
c.Set(optionBeeDebugApi, defaultBeeDebugApi)
c.Set(optionBeePostageBatchId, "")
c.Set(optionCookieDomain, defaultCookieDomain)

if err := c.WriteConfigAs(cfgFile); err != nil {
fmt.Println("failed to write config file")
os.Exit(1)
}
}
Loading

0 comments on commit ab15d5a

Please sign in to comment.