Skip to content

Commit b64e5bf

Browse files
committed
kepctl: Respect cobra directory layout
Signed-off-by: Stephen Augustus <[email protected]>
1 parent e8122df commit b64e5bf

File tree

5 files changed

+99
-38
lines changed

5 files changed

+99
-38
lines changed

cmd/kepctl/create.go renamed to cmd/kepctl/cmd/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package cmd
1818

1919
import (
2020
"github.com/spf13/cobra"

cmd/kepctl/promote.go renamed to cmd/kepctl/cmd/promote.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package cmd
1818

1919
import (
2020
"github.com/spf13/cobra"

cmd/kepctl/query.go renamed to cmd/kepctl/cmd/query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package cmd
1818

1919
import (
2020
"fmt"

cmd/kepctl/cmd/root.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
"os"
22+
23+
"github.com/sirupsen/logrus"
24+
"github.com/spf13/cobra"
25+
26+
"k8s.io/enhancements/pkg/kepctl"
27+
"k8s.io/release/pkg/log"
28+
)
29+
30+
// rootCmd represents the base command when called without any subcommands
31+
var rootCmd = &cobra.Command{
32+
Use: "kepctl",
33+
Short: "kepctl helps you build keps",
34+
PersistentPreRunE: initLogging,
35+
}
36+
37+
type rootOptions struct {
38+
logLevel string
39+
}
40+
41+
var rootOpts = &rootOptions{}
42+
43+
// Execute adds all child commands to the root command and sets flags appropriately.
44+
// This is called by main.main(). It only needs to happen once to the rootCmd.
45+
func Execute() {
46+
if err := rootCmd.Execute(); err != nil {
47+
logrus.Fatal(err)
48+
}
49+
}
50+
51+
func init() {
52+
rootCmd.PersistentFlags().StringVar(
53+
&rootOpts.logLevel,
54+
"log-level",
55+
"info",
56+
fmt.Sprintf("the logging verbosity, either %s", log.LevelNames()),
57+
)
58+
}
59+
60+
func initLogging(*cobra.Command, []string) error {
61+
return log.SetupGlobalLogger(rootOpts.logLevel)
62+
}
63+
64+
// TODO: Refactor/remove below
65+
66+
func main() {
67+
cmd, err := buildMainCommand()
68+
if err != nil {
69+
fmt.Fprintln(os.Stderr, err)
70+
os.Exit(1)
71+
}
72+
if err := cmd.Execute(); err != nil {
73+
fmt.Fprintln(os.Stderr, err)
74+
os.Exit(1)
75+
}
76+
}
77+
78+
func buildMainCommand() (*cobra.Command, error) {
79+
repoPath := os.Getenv("ENHANCEMENTS_PATH")
80+
k, err := kepctl.New(repoPath)
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
rootCmd := &cobra.Command{
86+
Use: "kepctl",
87+
Short: "kepctl helps you build keps",
88+
}
89+
90+
rootCmd.AddCommand(buildCreateCommand(k))
91+
rootCmd.AddCommand(buildPromoteCommand(k))
92+
rootCmd.AddCommand(buildQueryCommand(k))
93+
return rootCmd, nil
94+
}

cmd/kepctl/main.go

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,8 @@ limitations under the License.
1616

1717
package main
1818

19-
import (
20-
"fmt"
21-
"os"
22-
23-
"github.com/spf13/cobra"
24-
25-
"k8s.io/enhancements/pkg/kepctl"
26-
)
19+
import "k8s.io/enhancements/cmd/kepctl/cmd"
2720

2821
func main() {
29-
cmd, err := buildMainCommand()
30-
if err != nil {
31-
fmt.Fprintln(os.Stderr, err)
32-
os.Exit(1)
33-
}
34-
if err := cmd.Execute(); err != nil {
35-
fmt.Fprintln(os.Stderr, err)
36-
os.Exit(1)
37-
}
38-
}
39-
40-
func buildMainCommand() (*cobra.Command, error) {
41-
repoPath := os.Getenv("ENHANCEMENTS_PATH")
42-
k, err := kepctl.New(repoPath)
43-
if err != nil {
44-
return nil, err
45-
}
46-
47-
rootCmd := &cobra.Command{
48-
Use: "kepctl",
49-
Short: "kepctl helps you build keps",
50-
}
51-
52-
rootCmd.AddCommand(buildCreateCommand(k))
53-
rootCmd.AddCommand(buildPromoteCommand(k))
54-
rootCmd.AddCommand(buildQueryCommand(k))
55-
return rootCmd, nil
22+
cmd.Execute()
5623
}

0 commit comments

Comments
 (0)