1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the GPLv3, see LICENCE file for details.
13
"github.com/juju/errors"
14
"github.com/juju/persistent-cookiejar"
15
"github.com/juju/utils"
16
"gopkg.in/juju/charm.v6-unstable"
17
"gopkg.in/macaroon-bakery.v1/httpbakery"
18
"launchpad.net/gnuflag"
20
"github.com/juju/terms-client/api"
24
defaultTermServiceLocation = "http://localhost:8081"
25
readFile = ioutil.ReadFile
26
clientNew = func(options ...api.ClientOption) (api.Client, error) {
27
return api.NewClient(options...)
32
push-term is used to create a new Terms and Conditions document.
34
push-term text.txt user/enterprise-plan
35
creates a new Terms and Conditions with the content from
36
file text.txt and the name enterprise-plan and
37
returns the revision of the created document.
40
// NewPushTermCommand returns a new command that can be
41
// used to create new (revisions) of Terms and
42
// Conditions documents.
43
func NewPushTermCommand() *pushTermCommand {
44
return &pushTermCommand{}
47
// pushTermCommand creates a new Terms and Conditions document.
48
type pushTermCommand struct {
54
TermsServiceLocation string
57
// SetFlags implements Command.SetFlags.
58
func (c *pushTermCommand) SetFlags(f *gnuflag.FlagSet) {
59
// TODO (mattyw) Replace with JUJU_TERMS
60
f.StringVar(&c.TermsServiceLocation, "url", defaultTermServiceLocation, "url of the terms service")
61
c.out.AddFlags(f, "yaml", cmd.DefaultFormatters)
64
// Info implements Command.Info.
65
func (c *pushTermCommand) Info() *cmd.Info {
68
Args: "<filename> <term id>",
69
Purpose: "create new Terms and Conditions document (revision)",
74
// Init read and verifies the arguments.
75
func (c *pushTermCommand) Init(args []string) error {
77
return errors.New("missing arguments")
80
fn, id, args := args[0], args[1], args[2:]
82
if err := cmd.CheckEmpty(args); err != nil {
83
return errors.Errorf("unknown arguments: %v", strings.Join(args, ","))
91
// Run implements Command.Run.
92
func (c *pushTermCommand) Run(ctx *cmd.Context) error {
93
termid, err := charm.ParseTerm(c.TermID)
95
return errors.Annotatef(err, "invalid term id argument")
97
if termid.Revision > 0 {
98
return errors.Errorf("can't specify a revision with a new term")
100
if termid.Tenant != "" {
101
return errors.Errorf("can't specify a tenant with a new term")
103
data, err := readFile(c.TermFilename)
105
return errors.Annotatef(err, "could not read contents of %q", c.TermFilename)
108
jar, err := cookiejar.New(&cookiejar.Options{
109
Filename: cookieFile(),
112
return errors.Trace(err)
115
bakeryClient := httpbakery.NewClient()
116
bakeryClient.Jar = jar
117
bakeryClient.VisitWebPage = httpbakery.OpenWebBrowser
119
termsClient, err := clientNew(
120
api.ServiceURL(c.TermsServiceLocation),
121
api.HTTPClient(bakeryClient),
124
return errors.Trace(err)
127
response, err := termsClient.SaveTerm(
133
return errors.Trace(err)
136
err = c.out.Write(ctx, response)
138
return errors.Trace(err)
143
// cookieFile returns the path to the cookie used to store authorization
144
// macaroons. The returned value can be overridden by setting the
145
// JUJU_COOKIEFILE environment variable.
146
func cookieFile() string {
147
if file := os.Getenv("JUJU_COOKIEFILE"); file != "" {
150
return path.Join(utils.Home(), ".go-cookies")