import ( "encoding/json" "io" "github.com/juju/cmd" "github.com/juju/errors" "github.com/juju/gnuflag" "github.com/juju/juju/cmd/modelcmd" "github.com/juju/terms-client/api" "github.com/juju/terms-client/api/wireformat" "gopkg.in/macaroon-bakery.v1/httpbakery" ) var ( newClient = func(client *httpbakery.Client) (TermsServiceClient, error) { return api.NewClient(api.HTTPClient(client)) } ) // TermsServiceClient defines methods needed for the Terms Service CLI // commands. type TermsServiceClient interface { GetUsersAgreements() ([]wireformat.AgreementResponse, error) } const listAgreementsDoc = ` List terms the user has agreed to. ` // NewListAgreementsCommand returns a new command that can be // used to list agreements a user has made.
// Run implements Command.Run. func (c *agreeCommand) Run(ctx *cmd.Context) error { client, err := c.BakeryClient() if err != nil { return errors.Trace(err) } termsClient, err := clientNew(api.HTTPClient(client)) if err != nil { return err } if c.SkipTermContent { err := saveAgreements(ctx, termsClient, c.terms) if err != nil { return errors.Trace(err) } return nil } needAgreement := []wireformat.GetTermsResponse{} terms, err := termsClient.GetUnsignedTerms(&wireformat.CheckAgreementsRequest{ Terms: c.termIds, }) if err != nil { return errors.Annotate(err, "failed to retrieve terms") } needAgreement = append(needAgreement, terms...) if len(needAgreement) == 0 { fmt.Fprintf(ctx.Stdout, "Already agreed\n") return nil } err = printTerms(ctx, needAgreement) if err != nil { return errors.Trace(err) } fmt.Fprintf(ctx.Stdout, "Do you agree to the displayed terms? (Y/n): ") answer, err := userAnswer() if err != nil { return errors.Trace(err) } agreedTerms := make([]term, len(needAgreement)) for i, t := range needAgreement { agreedTerms[i] = term{owner: t.Owner, name: t.Name, revision: t.Revision} } answer = strings.TrimSpace(answer) if userAgrees(answer) { err = saveAgreements(ctx, termsClient, agreedTerms) if err != nil { return errors.Trace(err) } } else { fmt.Fprintf(ctx.Stdout, "You didn't agree to the presented terms.\n") return nil } return nil }