|
| 1 | +// Copyright (c) 2016 by António Meireles <antonio.meireles@reformi.st>. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "os/user" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/TheNewNormal/corectl/components/host/session" |
| 25 | + "github.com/TheNewNormal/corectl/release" |
| 26 | + "github.com/deis/pkg/log" |
| 27 | + "github.com/spf13/cobra" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + rootCmd = &cobra.Command{ |
| 32 | + Use: session.AppName(), |
| 33 | + Short: release.ShortBanner, |
| 34 | + Long: release.Banner, |
| 35 | + Run: func(cmd *cobra.Command, args []string) { |
| 36 | + cmd.UsageFunc()(cmd) |
| 37 | + }, |
| 38 | + SilenceUsage: true, |
| 39 | + SilenceErrors: true, |
| 40 | + } |
| 41 | + ErrNotEnoughRights = fmt.Errorf( |
| 42 | + "attempting a privileged operation without proper rights") |
| 43 | + ErrTooManyRights = fmt.Errorf("too many privileges invoking %v, "+ |
| 44 | + "please call it as a regular user", session.AppName()) |
| 45 | + ErrOwnerUnset = func(cmd *cobra.Command) error { |
| 46 | + return fmt.Errorf("attempting to call '%v' without "+ |
| 47 | + "setting a valid user (--user)", cmd.Name()) |
| 48 | + } |
| 49 | +) |
| 50 | + |
| 51 | +func init() { |
| 52 | + if session.AppName() != "corectld" { |
| 53 | + rootCmd.PersistentFlags().StringP("server", "s", "127.0.0.1", |
| 54 | + "corectld location") |
| 55 | + rootCmd.PersistentFlags().MarkHidden("server") |
| 56 | + } |
| 57 | + rootCmd.PersistentPreRunE = |
| 58 | + func(cmd *cobra.Command, args []string) (err error) { |
| 59 | + cli := session.Caller.CmdLine |
| 60 | + cli.BindPFlags(cmd.Flags()) |
| 61 | + log.DefaultLogger.SetDebug(cli.GetBool("debug")) |
| 62 | + if session.AppName() != "corectld" { |
| 63 | + if session.Caller.Privileged { |
| 64 | + return ErrTooManyRights |
| 65 | + } |
| 66 | + } else { |
| 67 | + if !session.Caller.Privileged && cmd.Name() == "uuid2mac" { |
| 68 | + return ErrNotEnoughRights |
| 69 | + } |
| 70 | + if session.Caller.Privileged { |
| 71 | + if cmd.Name() == "uuid2mac" { |
| 72 | + return |
| 73 | + } |
| 74 | + if cmd.Name() == "start" { |
| 75 | + var usr *user.User |
| 76 | + usr, err = user.Lookup(cli.GetString("user")) |
| 77 | + if err != nil { |
| 78 | + return ErrOwnerUnset(cmd) |
| 79 | + } |
| 80 | + session.Caller.User = usr |
| 81 | + } else { |
| 82 | + return ErrTooManyRights |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + if !(strings.HasPrefix(cmd.Name(), "genM") || |
| 87 | + cmd.Name() == "version") { |
| 88 | + if err = session.Caller.Network.SetContext(); err != nil { |
| 89 | + return |
| 90 | + } |
| 91 | + err = session.Caller.NormalizeOnDiskLayout() |
| 92 | + } |
| 93 | + return |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func defaultPreRunE(cmd *cobra.Command, args []string) error { |
| 98 | + if len(args) != 0 { |
| 99 | + return fmt.Errorf("Incorrect usage. " + |
| 100 | + "This command doesn't accept any arguments.") |
| 101 | + } |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +func viperStringSliceBugWorkaround(plain []string) []string { |
| 106 | + // getting around https://github.com/spf13/viper/issues/112 |
| 107 | + var sliced []string |
| 108 | + for _, x := range plain { |
| 109 | + strip := strings.Replace( |
| 110 | + strings.Replace(x, "]", "", -1), "[", "", -1) |
| 111 | + for _, y := range strings.Split(strip, ",") { |
| 112 | + sliced = append(sliced, y) |
| 113 | + } |
| 114 | + } |
| 115 | + return sliced |
| 116 | +} |
| 117 | + |
| 118 | +func main() { |
| 119 | + var err error |
| 120 | + if session.Caller, err = session.New(); err != nil { |
| 121 | + return |
| 122 | + } |
| 123 | + session.Caller.CmdLine.BindPFlags(rootCmd.PersistentFlags()) |
| 124 | + if session.AppName() != "corectld" { |
| 125 | + session.Caller.ServerAddress = |
| 126 | + session.Caller.CmdLine.GetString("server") + ":2511" |
| 127 | + } |
| 128 | + if err = rootCmd.Execute(); err != nil { |
| 129 | + log.Err(err.Error()) |
| 130 | + os.Exit(-1) |
| 131 | + } |
| 132 | +} |
0 commit comments