Skip to content

Commit

Permalink
Move main to root directory
Browse files Browse the repository at this point in the history
Change-Id: I33f190b5bf1d466474c7b1b97dc44c73524d6061
Reviewed-on: https://softwarefactory-project.io/r/5689
Reviewed-by: Sylvain Afchain <safchain@gmail.com>
Tested-by: Sylvain Afchain <safchain@gmail.com>
Workflow: Sylvain Afchain <safchain@gmail.com>
Tested-by: Jenkins CI <jenkins@softwarefactory-project.io>
  • Loading branch information
lebauce authored and Jenkins CI committed Dec 9, 2016
1 parent 4441d27 commit 0f80d25
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 78 deletions.
2 changes: 1 addition & 1 deletion cmd/agent/agent.go → cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
*/

package agent
package cmd

import (
"os"
Expand Down
2 changes: 1 addition & 1 deletion cmd/allinone/allinone.go → cmd/allinone.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
*/

package allinone
package cmd

import (
"os"
Expand Down
2 changes: 1 addition & 1 deletion cmd/analyzer/analyzer.go → cmd/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
*/

package analyzer
package cmd

import (
"os"
Expand Down
60 changes: 60 additions & 0 deletions cmd/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

package cmd

import (
"os"

"github.com/skydive-project/skydive/cmd/client"
"github.com/skydive-project/skydive/config"
"github.com/spf13/cobra"
)

var analyzerAddr string

var Client = &cobra.Command{
Use: "client",
Short: "Skydive client",
Long: "Skydive client",
SilenceUsage: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
cmd.Root().PersistentPreRun(cmd.Root(), args)
if analyzerAddr != "" {
config.GetConfig().Set("agent.analyzers", analyzerAddr)
} else {
config.GetConfig().SetDefault("agent.analyzers", "localhost:8082")
}
},
}

func init() {
Client.PersistentFlags().StringVarP(&client.AuthenticationOpts.Username, "username", "", os.Getenv("SKYDIVE_USERNAME"), "username auth parameter")
Client.PersistentFlags().StringVarP(&client.AuthenticationOpts.Password, "password", "", os.Getenv("SKYDIVE_PASSWORD"), "password auth parameter")
Client.PersistentFlags().StringVarP(&analyzerAddr, "analyzer", "", os.Getenv("SKYDIVE_ANALYZER"), "analyzer address")

Client.AddCommand(client.AlertCmd)
Client.AddCommand(client.CaptureCmd)
Client.AddCommand(client.TopologyCmd)
Client.AddCommand(client.ShellCmd)
Client.AddCommand(client.PacketInjectorCmd)
}
8 changes: 4 additions & 4 deletions cmd/client/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var AlertCreate = &cobra.Command{
Short: "Create alert",
Long: "Create alert",
Run: func(cmd *cobra.Command, args []string) {
client, err := api.NewCrudClientFromConfig(&authenticationOpts)
client, err := api.NewCrudClientFromConfig(&AuthenticationOpts)
if err != nil {
logging.GetLogger().Criticalf(err.Error())
}
Expand Down Expand Up @@ -83,7 +83,7 @@ var AlertList = &cobra.Command{
Long: "List alerts",
Run: func(cmd *cobra.Command, args []string) {
var alerts map[string]api.Alert
client, err := api.NewCrudClientFromConfig(&authenticationOpts)
client, err := api.NewCrudClientFromConfig(&AuthenticationOpts)
if err != nil {
logging.GetLogger().Criticalf(err.Error())
}
Expand All @@ -107,7 +107,7 @@ var AlertGet = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
var alert api.Alert
client, err := api.NewCrudClientFromConfig(&authenticationOpts)
client, err := api.NewCrudClientFromConfig(&AuthenticationOpts)
if err != nil {
logging.GetLogger().Criticalf(err.Error())
}
Expand All @@ -131,7 +131,7 @@ var AlertDelete = &cobra.Command{
}
},
Run: func(cmd *cobra.Command, args []string) {
client, err := api.NewCrudClientFromConfig(&authenticationOpts)
client, err := api.NewCrudClientFromConfig(&AuthenticationOpts)
if err != nil {
logging.GetLogger().Criticalf(err.Error())
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/client/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var CaptureCreate = &cobra.Command{
Short: "Create capture",
Long: "Create capture",
Run: func(cmd *cobra.Command, args []string) {
client, err := api.NewCrudClientFromConfig(&authenticationOpts)
client, err := api.NewCrudClientFromConfig(&AuthenticationOpts)
if err != nil {
logging.GetLogger().Criticalf(err.Error())
}
Expand Down Expand Up @@ -81,7 +81,7 @@ var CaptureList = &cobra.Command{
Long: "List captures",
Run: func(cmd *cobra.Command, args []string) {
var captures map[string]api.Capture
client, err := api.NewCrudClientFromConfig(&authenticationOpts)
client, err := api.NewCrudClientFromConfig(&AuthenticationOpts)
if err != nil {
logging.GetLogger().Criticalf(err.Error())
}
Expand All @@ -106,7 +106,7 @@ var CaptureGet = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
var capture api.Capture
client, err := api.NewCrudClientFromConfig(&authenticationOpts)
client, err := api.NewCrudClientFromConfig(&AuthenticationOpts)
if err != nil {
logging.GetLogger().Criticalf(err.Error())
}
Expand All @@ -130,7 +130,7 @@ var CaptureDelete = &cobra.Command{
}
},
Run: func(cmd *cobra.Command, args []string) {
client, err := api.NewCrudClientFromConfig(&authenticationOpts)
client, err := api.NewCrudClientFromConfig(&AuthenticationOpts)
if err != nil {
logging.GetLogger().Criticalf(err.Error())
}
Expand Down
31 changes: 1 addition & 30 deletions cmd/client/client.go → cmd/client/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,15 @@ import (
"fmt"
"os"

"github.com/skydive-project/skydive/config"
shttp "github.com/skydive-project/skydive/http"
"github.com/skydive-project/skydive/logging"
"github.com/spf13/cobra"
)

var (
authenticationOpts shttp.AuthenticationOpts
gremlinQuery string
analyzerAddr string
AuthenticationOpts shttp.AuthenticationOpts
)

var Client = &cobra.Command{
Use: "client",
Short: "Skydive client",
Long: "Skydive client",
SilenceUsage: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if flag := cmd.Flags().Lookup("analyzer"); !flag.Changed {
config.GetConfig().SetDefault("agent.analyzers", "localhost:8082")
} else {
config.GetConfig().Set("agent.analyzers", flag.Value)
}
},
}

func printJSON(obj interface{}) {
s, err := json.MarshalIndent(obj, "", " ")
if err != nil {
Expand All @@ -67,15 +50,3 @@ func setFromFlag(cmd *cobra.Command, flag string, value *string) {
*value = flag.Value.String()
}
}

func init() {
Client.PersistentFlags().StringVarP(&authenticationOpts.Username, "username", "", os.Getenv("SKYDIVE_USERNAME"), "username auth parameter")
Client.PersistentFlags().StringVarP(&authenticationOpts.Password, "password", "", os.Getenv("SKYDIVE_PASSWORD"), "password auth parameter")
Client.PersistentFlags().String("analyzer", "", "analyzer address")

Client.AddCommand(AlertCmd)
Client.AddCommand(CaptureCmd)
Client.AddCommand(TopologyCmd)
Client.AddCommand(ShellCmd)
Client.AddCommand(PacketInjectorCmd)
}
2 changes: 1 addition & 1 deletion cmd/client/packet_injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var PacketInjectorCmd = &cobra.Command{
Long: "inject packets",
SilenceUsage: false,
Run: func(cmd *cobra.Command, args []string) {
client, err := api.NewCrudClientFromConfig(&authenticationOpts)
client, err := api.NewCrudClientFromConfig(&AuthenticationOpts)
if err != nil {
logging.GetLogger().Criticalf(err.Error())
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/client/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (
"github.com/skydive-project/skydive/logging"
)

var gremlinQuery string

var TopologyCmd = &cobra.Command{
Use: "topology",
Short: "Request on topology",
Expand Down Expand Up @@ -77,7 +79,7 @@ var TopologyRequest = &cobra.Command{
Short: "query topology",
Long: "query topology",
Run: func(cmd *cobra.Command, args []string) {
body, err := SendGremlinQuery(&authenticationOpts, gremlinQuery)
body, err := SendGremlinQuery(&AuthenticationOpts, gremlinQuery)
if err != nil {
logging.GetLogger().Errorf(err.Error())
os.Exit(1)
Expand Down
56 changes: 21 additions & 35 deletions cmd/skydive/skydive.go → cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,60 +20,46 @@
*
*/

package main
package cmd

import (
"fmt"
"os"

"github.com/skydive-project/skydive/cmd/agent"
"github.com/skydive-project/skydive/cmd/allinone"
"github.com/skydive-project/skydive/cmd/analyzer"
"github.com/skydive-project/skydive/cmd/client"
"github.com/skydive-project/skydive/config"
"github.com/skydive-project/skydive/version"

"github.com/spf13/cobra"
)

var (
showVersion bool
cfgPath string
cfgBackend string
CfgPath string
CfgBackend string
)

var rootCmd = &cobra.Command{
func LoadConfiguration() {
if CfgPath != "" {
if err := config.InitConfig(CfgBackend, CfgPath); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
}

var RootCmd = &cobra.Command{
Use: "skydive [sub]",
Short: "Skydive",
SilenceUsage: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if cfgPath != "" {
if err := config.InitConfig(cfgBackend, cfgPath); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
},
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of Skydive",
Run: func(cmd *cobra.Command, args []string) {
version.PrintVersion()
LoadConfiguration()
},
}

func init() {
rootCmd.PersistentFlags().StringVarP(&cfgPath, "conf", "c", "", "location of Skydive agent config file")
rootCmd.PersistentFlags().StringVarP(&cfgBackend, "config-backend", "b", "file", "configuration backend (defaults to file)")
}
RootCmd.PersistentFlags().StringVarP(&CfgPath, "conf", "c", "", "location of Skydive agent config file")
RootCmd.PersistentFlags().StringVarP(&CfgBackend, "config-backend", "b", "file", "configuration backend (defaults to file)")

func main() {
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(agent.Agent)
rootCmd.AddCommand(analyzer.Analyzer)
rootCmd.AddCommand(client.Client)
rootCmd.AddCommand(allinone.AllInOne)
rootCmd.Execute()
RootCmd.AddCommand(VersionCmd)
RootCmd.AddCommand(Agent)
RootCmd.AddCommand(Analyzer)
RootCmd.AddCommand(Client)
RootCmd.AddCommand(AllInOne)
}
36 changes: 36 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

package cmd

import (
"github.com/skydive-project/skydive/version"
"github.com/spf13/cobra"
)

var VersionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of Skydive",
Run: func(cmd *cobra.Command, args []string) {
version.PrintVersion()
},
}
29 changes: 29 additions & 0 deletions skydive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

package main

import "github.com/skydive-project/skydive/cmd"

func main() {
cmd.RootCmd.Execute()
}

0 comments on commit 0f80d25

Please sign in to comment.