github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/cmd/grail-ssh/main.go (about)

     1  // The following enables go generate to generate the doc.go file.
     2  //go:generate go run v.io/x/lib/cmdline/gendoc "--build-cmd=go install" --copyright-notice= . -help
     3  
     4  package main
     5  
     6  import (
     7  	"flag"
     8  	"io"
     9  	"os"
    10  
    11  	"github.com/Schaudge/grailbase/cmdutil"
    12  	_ "github.com/Schaudge/grailbase/cmdutil/interactive" // print output to console
    13  	"github.com/Schaudge/grailbase/vcontext"
    14  	_ "github.com/grailbio/v23/factories/grail"
    15  	"v.io/v23/context"
    16  	"v.io/x/lib/cmdline"
    17  )
    18  
    19  var (
    20  	sshFlag   string
    21  	idRsaFlag string
    22  	userFlag  string
    23  )
    24  
    25  func newCmdRoot() *cmdline.Command {
    26  	root := &cmdline.Command{
    27  		Runner:   cmdutil.RunnerFuncWithAccessCheck(vcontext.Background, runner(runSsh)),
    28  		Name:     "ssh",
    29  		Short:    "ssh to a VM",
    30  		ArgsName: "<ticket>",
    31  		Long: `
    32  Command that simplifies connecting to GRAIL systems using ssh with ssh certificates for authentication.
    33  `,
    34  		LookPath: false,
    35  	}
    36  	root.Flags.StringVar(&sshFlag, "ssh", "ssh", "What ssh client to use")
    37  	root.Flags.StringVar(&idRsaFlag, "i", os.ExpandEnv("${HOME}/.ssh/id_rsa"), "Path to the SSH private key that will be used for the connection")
    38  	root.Flags.StringVar(&userFlag, "l", "", "Username to provide to the remote host. If not provided selects the first principal defined as part of the ticket definition.")
    39  
    40  	return root
    41  }
    42  
    43  type runnerFunc func(*context.T, io.Writer, *cmdline.Env, []string) error
    44  type v23RunnerFunc func(*context.T, *cmdline.Env, []string) error
    45  
    46  // runner wraps a runnerFunc to produce a cmdline.RunnerFunc.
    47  func runner(f runnerFunc) v23RunnerFunc {
    48  	return func(ctx *context.T, env *cmdline.Env, args []string) error {
    49  		// No special actions needed that applies to all runners.
    50  		return f(ctx, os.Stdout, env, args)
    51  	}
    52  }
    53  
    54  func main() {
    55  	// We suppress 'alsologtosterr' because this is a user tool.
    56  	_ = flag.Set("alsologtostderr", "false")
    57  	cmdRoot := newCmdRoot()
    58  	cmdline.HideGlobalFlagsExcept()
    59  	cmdline.Main(cmdRoot)
    60  }