Start starts the interactive shell
(rootCmd *cobra.Command)
| 37 | |
| 38 | // Start starts the interactive shell |
| 39 | func Start(rootCmd *cobra.Command) { |
| 40 | fmt.Println("PikPak CLI Interactive Shell") |
| 41 | fmt.Println("Type 'help' for available commands, 'exit' or Ctrl-D to quit") |
| 42 | fmt.Println() |
| 43 | |
| 44 | currentPath := "/" |
| 45 | |
| 46 | p := api.NewPikPak(conf.Config.Username, conf.Config.Password) |
| 47 | if err := p.Login(); err != nil { |
| 48 | fmt.Println("Login failed") |
| 49 | logx.Error(err) |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | l, err := readline.NewEx(&readline.Config{ |
| 54 | Prompt: promptForPath(currentPath), |
| 55 | AutoComplete: &shellAutoCompleter{ |
| 56 | rootCmd: rootCmd, |
| 57 | fileStatSource: &p, |
| 58 | currentPath: func() string { |
| 59 | return currentPath |
| 60 | }, |
| 61 | }, |
| 62 | }) |
| 63 | if err != nil { |
| 64 | fmt.Println("Initialize readline failed") |
| 65 | logx.Error(err) |
| 66 | return |
| 67 | } |
| 68 | defer l.Close() |
| 69 | |
| 70 | for { |
| 71 | input, err := l.Readline() |
| 72 | |
| 73 | if isReadlineInterrupt(err) { |
| 74 | fmt.Println() |
| 75 | l.SetPrompt(promptForPath(currentPath)) |
| 76 | continue |
| 77 | } |
| 78 | |
| 79 | if shouldExitOnReadlineError(err) { |
| 80 | fmt.Println("\nBye~!") |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | if err != nil { |
| 85 | fmt.Println("\nBye~!") |
| 86 | break |
| 87 | } |
| 88 | |
| 89 | input = strings.TrimSpace(input) |
| 90 | if input == "" { |
| 91 | continue |
| 92 | } |
| 93 | |
| 94 | args := parseShellArgs(input) |
| 95 | if len(args) == 0 { |
| 96 | continue |
nothing calls this directly
no test coverage detected