Skip to content

Sessions

GGP1 edited this page Jul 28, 2024 · 8 revisions

The session command is, essentially, a wrapper of the root command and all its subcommands, with the difference that it doesn't exit after executing them. This makes sessions great for executing multiple commands passing the master password only once, as explained in memory security, this is completely secure.

To start a session use kure session.

You can set a timeout using the [-t timeout] flag so it will automatically close the session once the time specified has passed.

For more details, check out the command's documentation.

Session commands

  • block: block execution (requires to be manually unlocked).
  • exit|quit|Ctrl+C: close the session.
  • pwd: show current directory.
  • sleep: sleep for x time.
  • timeout: show time left.

Demo

Here is a simple demo of how session works with some file commands.

Session

Implementation

Here's a simplified implementation of session.go:

func runSession(cmd *cobra.Command, r io.Reader, opts *sessionOptions) {
	timeout := &timeout{
		t:     opts.timeout,
		start: time.Now(),
		timer: time.NewTimer(opts.timeout),
	}

	go startSession(cmd, r, opts.prefix, timeout)

	if timeout.t == 0 {
		timeout.timer.Stop()
	}

	<-timeout.timer.C
}

func startSession(cmd *cobra.Command, r io.Reader, prefix string, timeout *timeout) {
	reader := bufio.NewReader(r)
  	root := cmd.Root()

	for {
		fmt.Printf("%s ", prefix)

		text, _, err := reader.ReadLine()
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			continue
		}

		args := strings.Split(string(text), " ")

		if err := execute(root, args, timeout); err != nil {
			fmt.Fprintln(os.Stderr, err)
		}
	}
}