update-userdata-devtool #58
| @ -77,10 +77,10 @@ func main() { | ||||
| 	pfp := path.Join(scriptDir, "pp.csv") | ||||
| 
 | ||||
| 	cfg := engine.Config{ | ||||
| 		Root:          "root", | ||||
| 		OutputSize:    uint32(size), | ||||
| 		FlagCount:     uint32(128), | ||||
| 		MenuSeparator: menuSeparator, | ||||
| 		Root:              "root", | ||||
| 		OutputSize:        uint32(size), | ||||
| 		FlagCount:         uint32(128), | ||||
| 		MenuSeparator:     menuSeparator, | ||||
| 		ResetOnEmptyInput: true, | ||||
| 	} | ||||
| 
 | ||||
|  | ||||
| @ -92,10 +92,10 @@ func main() { | ||||
| 	pfp := path.Join(scriptDir, "pp.csv") | ||||
| 
 | ||||
| 	cfg := engine.Config{ | ||||
| 		Root:          "root", | ||||
| 		OutputSize:    uint32(size), | ||||
| 		FlagCount:     uint32(128), | ||||
| 		MenuSeparator: menuSeparator, | ||||
| 		Root:              "root", | ||||
| 		OutputSize:        uint32(size), | ||||
| 		FlagCount:         uint32(128), | ||||
| 		MenuSeparator:     menuSeparator, | ||||
| 		ResetOnEmptyInput: true, | ||||
| 	} | ||||
| 
 | ||||
|  | ||||
| @ -78,10 +78,10 @@ func main() { | ||||
| 	pfp := path.Join(scriptDir, "pp.csv") | ||||
| 
 | ||||
| 	cfg := engine.Config{ | ||||
| 		Root:          "root", | ||||
| 		OutputSize:    uint32(size), | ||||
| 		FlagCount:     uint32(128), | ||||
| 		MenuSeparator: menuSeparator, | ||||
| 		Root:              "root", | ||||
| 		OutputSize:        uint32(size), | ||||
| 		FlagCount:         uint32(128), | ||||
| 		MenuSeparator:     menuSeparator, | ||||
| 		ResetOnEmptyInput: true, | ||||
| 	} | ||||
| 
 | ||||
|  | ||||
| @ -81,9 +81,9 @@ func main() { | ||||
| 	pfp := path.Join(scriptDir, "pp.csv") | ||||
| 
 | ||||
| 	cfg := engine.Config{ | ||||
| 		Root:       "root", | ||||
| 		OutputSize: uint32(size), | ||||
| 		FlagCount:  uint32(128), | ||||
| 		Root:              "root", | ||||
| 		OutputSize:        uint32(size), | ||||
| 		FlagCount:         uint32(128), | ||||
| 		ResetOnEmptyInput: true, | ||||
| 	} | ||||
| 	if stateDebug { | ||||
|  | ||||
| @ -25,7 +25,7 @@ const ( | ||||
| 	defaultSSHHost  string = "127.0.0.1" | ||||
| 	defaultSSHPort  uint   = 7122 | ||||
| 	defaultHTTPHost string = "127.0.0.1" | ||||
| 	defaultHTTPPort uint     = 7123 | ||||
| 	defaultHTTPPort uint   = 7123 | ||||
| 	defaultDomain          = "sarafu.local" | ||||
| ) | ||||
| 
 | ||||
| @ -52,7 +52,6 @@ func SearchDomains() []string { | ||||
| 	return ParsedDomains | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| func Language() string { | ||||
| 	return viseconfig.DefaultLanguage | ||||
| } | ||||
|  | ||||
| @ -7,8 +7,8 @@ import ( | ||||
| 	"os" | ||||
| 	"path" | ||||
| 
 | ||||
| 	"git.defalsify.org/vise.git/logging" | ||||
| 	"git.defalsify.org/vise.git/engine" | ||||
| 	"git.defalsify.org/vise.git/logging" | ||||
| 
 | ||||
| 	"git.grassecon.net/grassrootseconomics/sarafu-vise/config" | ||||
| 	"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers/application" | ||||
| @ -17,8 +17,8 @@ import ( | ||||
| ) | ||||
| 
 | ||||
| var ( | ||||
| 	logg      = logging.NewVanilla().WithContextKey("SessionId") | ||||
| 	scriptDir = path.Join("services", "registration") | ||||
| 	logg          = logging.NewVanilla().WithContextKey("SessionId") | ||||
| 	scriptDir     = path.Join("services", "registration") | ||||
| 	menuSeparator = ": " | ||||
| ) | ||||
| 
 | ||||
| @ -87,5 +87,4 @@ func main() { | ||||
| 		fmt.Fprintf(os.Stderr, "cmd exec error: %v\n", err) | ||||
| 		os.Exit(1) | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -3,9 +3,11 @@ package cmd | ||||
| import ( | ||||
| 	"context" | ||||
| 	"fmt" | ||||
| 	"regexp" | ||||
| 
 | ||||
| 	"git.defalsify.org/vise.git/logging" | ||||
| 	"git.defalsify.org/vise.git/db" | ||||
| 	"git.defalsify.org/vise.git/engine" | ||||
| 	"git.defalsify.org/vise.git/logging" | ||||
| 	"git.defalsify.org/vise.git/persist" | ||||
| 	"git.defalsify.org/vise.git/resource" | ||||
| 	"git.defalsify.org/vise.git/state" | ||||
| @ -16,21 +18,24 @@ import ( | ||||
| var argc map[string]int = map[string]int{ | ||||
| 	"reset": 0, | ||||
| 	"admin": 1, | ||||
| 	"clone": 1, | ||||
| } | ||||
| 
 | ||||
| var ( | ||||
| 	logg = logging.NewVanilla().WithDomain("cmd").WithContextKey("SessionId") | ||||
| 	logg             = logging.NewVanilla().WithDomain("cmd").WithContextKey("SessionId") | ||||
| 	cloneTargetRegex = `^\+000` | ||||
| ) | ||||
| 
 | ||||
| type Cmd struct { | ||||
| 	sessionId  string | ||||
| 	conn       storage.ConnData | ||||
| 	flagParser *application.FlagManager | ||||
| 	cmd        int | ||||
| 	enable     bool | ||||
| 	exec       func(ctx context.Context, ss storage.StorageService) error | ||||
| 	engineConfig	*engine.Config | ||||
| 	st	*state.State | ||||
| 	sessionId    string | ||||
| 	conn         storage.ConnData | ||||
| 	flagParser   *application.FlagManager | ||||
| 	cmd          int | ||||
| 	enable       bool | ||||
| 	param        string | ||||
| 	exec         func(ctx context.Context, ss storage.StorageService) error | ||||
| 	engineConfig *engine.Config | ||||
| 	st           *state.State | ||||
| } | ||||
| 
 | ||||
| func NewCmd(sessionId string, flagParser *application.FlagManager) *Cmd { | ||||
| @ -70,6 +75,50 @@ func (c *Cmd) engine(ctx context.Context, rs resource.Resource, pe *persist.Pers | ||||
| 	return en, nil | ||||
| } | ||||
| 
 | ||||
| func (c *Cmd) execClone(ctx context.Context, ss storage.StorageService) error { | ||||
| 	re := regexp.MustCompile(cloneTargetRegex) | ||||
| 	if !re.MatchString(c.param) { | ||||
| 		return fmt.Errorf("Clone sessionId must match target: %s", c.param) | ||||
| 	} | ||||
| 
 | ||||
| 	pe, err := ss.GetPersister(ctx) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("get persister error: %v", err) | ||||
| 	} | ||||
| 	err = pe.Load(c.engineConfig.SessionId) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("persister load error: %v", err) | ||||
| 	} | ||||
| 
 | ||||
| 	/// TODO consider DRY with devtools/store/dump
 | ||||
| 	store, err := ss.GetUserdataDb(ctx) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("store retrieve error: %v", err) | ||||
| 	} | ||||
| 
 | ||||
| 	store.SetSession(c.engineConfig.SessionId) | ||||
| 	store.SetPrefix(db.DATATYPE_USERDATA) | ||||
| 	dmp, err := store.Dump(ctx, []byte("")) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("store dump fail: %v\n", err.Error()) | ||||
| 	} | ||||
| 
 | ||||
| 	for true { | ||||
| 		store.SetSession(c.engineConfig.SessionId) | ||||
| 		k, v := dmp.Next(ctx) | ||||
| 		if k == nil { | ||||
| 			break | ||||
| 		} | ||||
| 		store.SetSession(c.param) | ||||
| 		err = store.Put(ctx, k, v) | ||||
| 		if err != nil { | ||||
| 			return fmt.Errorf("user data store clone failed on key: %x", k) | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	return pe.Save(c.param) | ||||
| } | ||||
| 
 | ||||
| func (c *Cmd) execReset(ctx context.Context, ss storage.StorageService) error { | ||||
| 	pe, err := ss.GetPersister(ctx) | ||||
| 	if err != nil { | ||||
| @ -157,6 +206,16 @@ func (c *Cmd) parseCmdReset(cmd string, param string, more []string) (bool, erro | ||||
| 	return false, nil | ||||
| } | ||||
| 
 | ||||
| func (c *Cmd) parseCmdClone(cmd string, param string, more []string) (bool, error) { | ||||
| 	if cmd == "clone" { | ||||
| 		c.enable = false | ||||
| 		c.param = param | ||||
| 		c.exec = c.execClone | ||||
| 		return true, nil | ||||
| 	} | ||||
| 	return false, nil | ||||
| } | ||||
| 
 | ||||
| func (c *Cmd) Parse(args []string) error { | ||||
| 	var param string | ||||
| 	if len(args) < 1 { | ||||
| @ -169,7 +228,7 @@ func (c *Cmd) Parse(args []string) error { | ||||
| 		return fmt.Errorf("invalid command: %v", cmd) | ||||
| 	} | ||||
| 	if n > 0 { | ||||
| 		if len(args) < n + 1 { | ||||
| 		if len(args) < n+1 { | ||||
| 			return fmt.Errorf("Wrong number of arguments, need: %d", n) | ||||
| 		} | ||||
| 		param = args[1] | ||||
| @ -192,6 +251,13 @@ func (c *Cmd) Parse(args []string) error { | ||||
| 		return nil | ||||
| 	} | ||||
| 
 | ||||
| 	r, err = c.parseCmdClone(cmd, param, args) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	if r { | ||||
| 		return nil | ||||
| 	} | ||||
| 
 | ||||
| 	return fmt.Errorf("unknown subcommand: %s", cmd) | ||||
| } | ||||
|  | ||||
| @ -10,9 +10,9 @@ import ( | ||||
| func TestInsertOrShift(t *testing.T) { | ||||
| 	tests := []struct { | ||||
| 		name     string | ||||
| 		profile Profile | ||||
| 		index   int | ||||
| 		value   string | ||||
| 		profile  Profile | ||||
| 		index    int | ||||
| 		value    string | ||||
| 		expected []string | ||||
| 	}{ | ||||
| 		{ | ||||
|  | ||||
| @ -181,8 +181,8 @@ func (s *SshRunner) GetEngine(sessionId string) (engine.Engine, func(), error) { | ||||
| 	accountService := services.New(ctx, menuStorageService) | ||||
| 	_, err = lhs.GetHandler(accountService) | ||||
| 	if err != nil { | ||||
| 	       fmt.Fprintf(os.Stderr, "get accounts service handler: %v\n", err) | ||||
| 	       os.Exit(1) | ||||
| 		fmt.Fprintf(os.Stderr, "get accounts service handler: %v\n", err) | ||||
| 		os.Exit(1) | ||||
| 	} | ||||
| 	en := lhs.GetEngine(lhs.Cfg, rs, pe) | ||||
| 	closer := func() { | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user