github.com/mutagen-io/mutagen@v0.18.0-rc1/cmd/mutagen/prompt.go (about) 1 package main 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "os" 8 9 "github.com/mutagen-io/mutagen/cmd/mutagen/daemon" 10 11 promptingpkg "github.com/mutagen-io/mutagen/pkg/prompting" 12 promptingsvc "github.com/mutagen-io/mutagen/pkg/service/prompting" 13 ) 14 15 // promptMain is the entry point for prompting. 16 func promptMain(arguments []string) error { 17 // Extract prompt. 18 if len(arguments) != 1 { 19 return errors.New("invalid number of arguments") 20 } 21 prompt := arguments[0] 22 23 // Extract environment parameters. 24 prompter := os.Getenv(promptingpkg.PrompterEnvironmentVariable) 25 if prompter == "" { 26 return errors.New("no prompter specified") 27 } 28 29 // Connect to the daemon and defer closure of the connection. 30 daemonConnection, err := daemon.Connect(false, true) 31 if err != nil { 32 return fmt.Errorf("unable to connect to daemon: %w", err) 33 } 34 defer daemonConnection.Close() 35 36 // Create a prompt service client. 37 promptingService := promptingsvc.NewPromptingClient(daemonConnection) 38 39 // Invoke prompt. 40 request := &promptingsvc.PromptRequest{ 41 Prompter: prompter, 42 Prompt: prompt, 43 } 44 response, err := promptingService.Prompt(context.Background(), request) 45 if err != nil { 46 return fmt.Errorf("unable to invoke prompt: %w", err) 47 } else if err = response.EnsureValid(); err != nil { 48 return fmt.Errorf("invalid prompt response: %w", err) 49 } 50 51 // Print the response. 52 fmt.Println(response.Response) 53 54 // Success. 55 return nil 56 }