github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/cmd/dosa/scope.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package main 22 23 import ( 24 "context" 25 "fmt" 26 27 "github.com/pkg/errors" 28 "github.com/uber-go/dosa" 29 ) 30 31 // ScopeOptions contains configuration for scope command flags 32 type ScopeOptions struct{} 33 34 // ScopeCmd is a placeholder for all scope commands 35 type ScopeCmd struct{} 36 37 func (c *ScopeCmd) doScopeOp(name string, f func(dosa.AdminClient, context.Context, string) error, scopes []string) error { 38 // set default service name if one isn't provided, this is done here instead 39 // of in the struct tags because schema and scope commands differ slightly 40 // in how the service name should be inferred. 41 if options.ServiceName == "" { 42 options.ServiceName = _defServiceName // defined in options.go 43 } 44 45 client, err := getAdminClient(options) 46 if err != nil { 47 return err 48 } 49 for _, s := range scopes { 50 ctx, cancel := context.WithTimeout(context.Background(), options.Timeout.Duration()) 51 defer cancel() 52 if err := f(client, ctx, s); err != nil { 53 return errors.Wrapf(err, "%s scope on %q", name, s) 54 } 55 fmt.Printf("%s scope %q: OK\n", name, s) 56 } 57 return nil 58 } 59 60 // ScopeCreate contains data for executing scope create command. 61 type ScopeCreate struct { 62 *ScopeCmd 63 Args struct { 64 Scopes []string `positional-arg-name:"scopes" required:"1"` 65 } `positional-args:"yes" required:"1"` 66 } 67 68 // Execute executes a scope create command 69 func (c *ScopeCreate) Execute(args []string) error { 70 return c.doScopeOp("create", dosa.AdminClient.CreateScope, c.Args.Scopes) 71 } 72 73 // ScopeDrop contains data for executing scope drop command. 74 type ScopeDrop struct { 75 *ScopeCmd 76 Args struct { 77 Scopes []string `positional-arg-name:"scopes" required:"1"` 78 } `positional-args:"yes" required:"1"` 79 } 80 81 // Execute executes a scope drop command 82 func (c *ScopeDrop) Execute(args []string) error { 83 return c.doScopeOp("drop", dosa.AdminClient.DropScope, c.Args.Scopes) 84 } 85 86 // ScopeTruncate contains data for executing scope truncate command. 87 type ScopeTruncate struct { 88 *ScopeCmd 89 Args struct { 90 Scopes []string `positional-arg-name:"scopes" required:"1"` 91 } `positional-args:"yes" required:"1"` 92 } 93 94 // Execute executes a scope truncate command 95 func (c *ScopeTruncate) Execute(args []string) error { 96 return c.doScopeOp("truncate", dosa.AdminClient.TruncateScope, c.Args.Scopes) 97 }