Skip to content
Snippets Groups Projects
Commit 7030337e authored by Manuel Kieweg's avatar Manuel Kieweg
Browse files

simple cli for dev functions

parent 2f66eaba
No related branches found
No related tags found
3 merge requests!117Cli,!116Resolve "Transport Tests",!90Develop
author: da/net research group <danet.fbi.h-da.de>
year: 2021
license: BSD
BSD 3-Clause License Copyright © 2021 da/net research group <danet.fbi.h-da.de>
Copyright (c) 2020, da/net
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this 1. Redistributions of source code must retain the above copyright notice,
list of conditions and the following disclaimer. this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, 2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution. and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its 3. Neither the name of the copyright holder nor the names of its contributors
contributors may be used to endorse or promote products derived from may be used to endorse or promote products derived from this software
this software without specific prior written permission. without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
......
package cli
import (
"code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi"
"code.fbi.h-da.de/cocsn/gosdn/nucleus"
"context"
"fmt"
gpb "github.com/openconfig/gnmi/proto/gnmi"
"strings"
)
func Capabilities() error {
cfg := gnmi.Config{
Addr: "portainer.danet.fbi.h-da.de:6030",
Username: "admin",
Password: "arista",
Encoding: gpb.Encoding_JSON_IETF,
}
opts := &nucleus.GnmiTransportOptions{Config: cfg}
transport, err := nucleus.NewGnmiTransport(opts)
if err != nil {
return err
}
resp, err := transport.Capabilities(context.Background())
if err != nil {
return err
}
modelData := resp.(*gpb.CapabilityResponse).SupportedModels
b := strings.Builder{}
for _, elem := range modelData {
_, err := b.WriteString(elem.Name)
if err != nil {
return err
}
_, err = b.WriteString("\n")
if err != nil {
return err
}
}
fmt.Println(b.String())
return nil
}
package cli
import (
"code.fbi.h-da.de/cocsn/gosdn/nucleus"
log "github.com/sirupsen/logrus"
"os"
)
func Gosdn() error {
_, debug := os.LookupEnv("GOSDN_DEBUG")
if debug {
log.SetLevel(log.DebugLevel)
}
// Setup a channel to communicate if goSDN should shutdown.
IsRunningChannel := make(chan bool)
// hand off to cmd for further processing
return nucleus.StartAndRun(IsRunningChannel)
}
package cli
import (
"code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi"
"code.fbi.h-da.de/cocsn/gosdn/nucleus"
"github.com/google/uuid"
gpb "github.com/openconfig/gnmi/proto/gnmi"
log "github.com/sirupsen/logrus"
)
/*
Simple gnmi request program. Use with cauton and leaf paths only.
Bootstrapping of pnd, device and transport simplified not idiomatic
*/
func Get() error {
sbi := &nucleus.OpenConfig{}
opts := &nucleus.GnmiTransportOptions{
Config: gnmi.Config{
Addr: "portainer.danet.fbi.h-da.de:6030",
Username: "admin",
Password: "arista",
Encoding: gpb.Encoding_JSON_IETF,
},
SetNode: sbi.SetNode(),
}
device, err := nucleus.NewDevice(sbi, opts)
if err != nil {
return err
}
pnd, err := nucleus.NewPND("openconfig", "test description", uuid.New(), sbi)
if err != nil {
return err
}
if err := pnd.AddDevice(device); err != nil {
return err
}
p := []string{"/interfaces/interface"}
errors := 0
for _, path := range p {
err := pnd.RequestAll(path)
if err != nil {
log.Debug(err)
errors++
break
}
}
percentage := float64(errors) / float64(len(p)) * 100.0
if errors != 0 {
log.Error("%v errors", errors)
log.Error("%v percent failed", percentage)
}
return nil
}
package cli
import (
model "code.fbi.h-da.de/cocsn/yang-models/generated/arista"
"github.com/openconfig/ygot/ytypes"
log "github.com/sirupsen/logrus"
)
var schema *ytypes.Schema
func init(){
var err error
schema, err = model.Schema()
if err != nil {
log.Fatal(err)
}
}
\ No newline at end of file
package cli
import (
"code.fbi.h-da.de/cocsn/gosdn/nucleus/util"
"code.fbi.h-da.de/cocsn/yang-models/generated/openconfig"
log "github.com/sirupsen/logrus"
)
func PathTraversal() error {
log.SetLevel(log.DebugLevel)
schema, _ := openconfig.Schema()
paths := util.NewPaths()
if err := paths.ParseSchema(schema, "device"); err != nil {
return err
}
for _, v := range paths {
v.Print()
}
p := paths.StringBuilder()
log.Debug(p)
return nil
}
package cli
import (
"code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi"
"code.fbi.h-da.de/cocsn/gosdn/nucleus"
"code.fbi.h-da.de/cocsn/gosdn/nucleus/util"
"context"
"github.com/golang/protobuf/proto"
"os"
)
func Set() error {
a := os.Getenv("GOSDN_TEST_ENDPOINT")
if a == "" {
a = "portainer.danet.fbi.h-da.de:6030"
}
opts := &nucleus.GnmiTransportOptions{
Config: gnmi.Config{
Addr: a,
Username: "admin",
Password: "arista",
},
}
t, err := nucleus.NewGnmiTransport(opts)
if err != nil {
return err
}
reqs := []interface{}{
&gnmi.Operation{
Type: "update",
Origin: "",
Target: "",
Path: []string{
"system",
"config",
"hostname",
},
Val: "ceos3000",
},
}
resp, err := t.Set(context.Background(), reqs...)
if err != nil {
return err
}
if err := util.Write(resp.(proto.Message), "resp-set-system-config-hostname"); err != nil {
return err
}
return nil
}
package cli
import (
"code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi"
"code.fbi.h-da.de/cocsn/gosdn/nucleus"
"context"
"fmt"
"github.com/google/uuid"
gpb "github.com/openconfig/gnmi/proto/gnmi"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"syscall"
"time"
)
func Subscribe() error{
log.SetLevel(log.DebugLevel)
sbi := &nucleus.OpenConfig{}
device, err := nucleus.NewDevice(sbi,
&nucleus.GnmiTransportOptions{
Config: gnmi.Config{
Addr: "portainer.danet.fbi.h-da.de:6030",
Username: "admin",
Password: "arista",
Encoding: gpb.Encoding_JSON_IETF,
},
SetNode: sbi.SetNode(),
RespChan: make(chan *gpb.SubscribeResponse),
})
if err != nil {
return err
}
pnd, err := nucleus.NewPND("openconfig", "a simple openconfig PND", uuid.New(), sbi)
if err != nil {
return err
}
if err := pnd.AddDevice(device); err != nil {
return err
}
paths := []string{"/interfaces/interface/name"}
opts := &gnmi.SubscribeOptions{
UpdatesOnly: false,
Prefix: "",
Mode: "stream",
StreamMode: "sample",
SampleInterval: uint64(10 * time.Second.Nanoseconds()),
SuppressRedundant: false,
HeartbeatInterval: uint64(time.Second.Nanoseconds()),
Paths: gnmi.SplitPaths(paths),
Origin: "",
Target: "portainer.danet.fbi.h-da.de:6030",
}
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGILL, syscall.SIGTERM)
ctx := context.WithValue(context.Background(), "opts", opts)
go func() {
if err := device.Transport.Subscribe(ctx); err != nil {
log.Fatal(err)
}
}()
fmt.Println("awaiting signal")
<-done
fmt.Println("exiting")
return nil
}
package cli
import (
"code.fbi.h-da.de/cocsn/gosdn/forks/google/gnmi"
"code.fbi.h-da.de/cocsn/gosdn/forks/google/gnmi/modeldata"
oc "code.fbi.h-da.de/cocsn/yang-models/generated/arista"
"context"
"flag"
"github.com/google/gnxi/utils/credentials"
pb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/ygot/ygot"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
"net"
"reflect"
)
var (
bindAddr = flag.String("bind_address", ":9339", "Bind to address:port or just :port")
)
type server struct {
*gnmi.Server
}
func callback(newConfig ygot.ValidatedGoStruct) error {
// Apply the config to your device and return nil if success. return error if fails.
//
// Do something ...
return nil
}
func newServer(model *gnmi.Model, config []byte) (*server, error) {
s, err := gnmi.NewServer(model, config, callback)
if err != nil {
return nil, err
}
return &server{Server: s}, nil
}
// Get overrides the Get func of gnmi.Target to provide user auth.
func (s *server) Get(ctx context.Context, req *pb.GetRequest) (*pb.GetResponse, error) {
msg, ok := credentials.AuthorizeUser(ctx)
if !ok {
log.Infof("denied a Get request: %v", msg)
return nil, status.Error(codes.PermissionDenied, msg)
}
log.Infof("allowed a Get request: %v", msg)
return s.Server.Get(ctx, req)
}
// Set overrides the Set func of gnmi.Target to provide user auth.
/*
func (s *server) Set(ctx context.Context, req *pb.SetRequest) (*pb.SetResponse, error) {
msg, ok := credentials.AuthorizeUser(ctx)
if !ok {
log.Infof("denied a Set request: %v", msg)
return nil, status.Error(codes.PermissionDenied, msg)
}
log.Infof("allowed a Set request: %v", msg)
return s.Server.Set(ctx, req)
}
*/
func Target() error {
// Google stuff from here
model := gnmi.NewModel(modeldata.ModelData,
reflect.TypeOf((*oc.Device)(nil)),
oc.SchemaTree["Device"],
oc.Unmarshal,
oc.ΛEnum)
g := grpc.NewServer()
var configData []byte
s, err := newServer(model, configData)
if err != nil {
return err
}
pb.RegisterGNMIServer(g, s)
reflection.Register(g)
log.Infof("starting to listen on %s", *bindAddr)
listen, err := net.Listen("tcp", *bindAddr)
if err != nil {
return err
}
log.Info("starting to serve")
if err := g.Serve(listen); err != nil {
return err
}
return nil
}
package cli
import (
log "github.com/golang/glog"
"github.com/openconfig/ygot/util"
)
func LeafPaths() error {
for _,v := range schema.SchemaTree {
entry, err := util.FindLeafRefSchema(v, "/interface/")
if err != nil {
log.Error(err)
}
log.Info(entry)
}
return nil
}
\ No newline at end of file
/*
Copyright © 2021 da/net research group <danet.fbi.h-da.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package cmd
import (
"code.fbi.h-da.de/cocsn/gosdn/cli"
"github.com/spf13/cobra"
)
// capabilitiesCmd represents the capabilities command
var capabilitiesCmd = &cobra.Command{
Use: "capabilities",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
RunE: func(cmd *cobra.Command, args []string) error {
return cli.Capabilities()
},
}
func init() {
rootCmd.AddCommand(capabilitiesCmd)
}
/*
Copyright © 2021 da/net research group <danet.fbi.h-da.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package cmd
import (
"code.fbi.h-da.de/cocsn/gosdn/cli"
"github.com/spf13/cobra"
)
// getCmd represents the get command
var getCmd = &cobra.Command{
Use: "get",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
RunE: func(cmd *cobra.Command, args []string) error {
return cli.Get()
},
}
func init() {
rootCmd.AddCommand(getCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// getCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// getCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
File added
package main /*
Copyright © 2021 da/net research group <danet.fbi.h-da.de>
All rights reserved.
import ( Redistribution and use in source and binary forms, with or without
"code.fbi.h-da.de/cocsn/gosdn/nucleus" modification, are permitted provided that the following conditions are met:
log "github.com/sirupsen/logrus"
"os"
)
func main() { 1. Redistributions of source code must retain the above copyright notice,
_, debug := os.LookupEnv("GOSDN_DEBUG") this list of conditions and the following disclaimer.
if debug {
log.SetLevel(log.DebugLevel)
}
// Setup a channel to communicate if goSDN should shutdown. 2. Redistributions in binary form must reproduce the above copyright notice,
IsRunningChannel := make(chan bool) this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
// hand off to cmd for further processing 3. Neither the name of the copyright holder nor the names of its contributors
nucleus.StartAndRun(IsRunningChannel) may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package main
import "code.fbi.h-da.de/cocsn/gosdn/cmd"
func main() {
cmd.Execute()
} }
/*
Copyright © 2021 da/net research group <danet.fbi.h-da.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package cmd
import (
"code.fbi.h-da.de/cocsn/gosdn/cli"
"github.com/spf13/cobra"
)
// legacyCmd represents the legacy command
var legacyCmd = &cobra.Command{
Use: "legacy",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
RunE: func(cmd *cobra.Command, args []string) error {
return cli.PathTraversal()
},
}
func init() {
pathCmd.AddCommand(legacyCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// legacyCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// legacyCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
/*
Copyright © 2021 da/net research group <danet.fbi.h-da.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package cmd
import (
"errors"
"github.com/spf13/cobra"
)
// pathCmd represents the path command
var pathCmd = &cobra.Command{
Use: "path",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
RunE: func(cmd *cobra.Command, args []string) error {
return errors.New("path needs either [ygot] or [legacy] command")
},
}
func init() {
rootCmd.AddCommand(pathCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// pathCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// pathCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
/*
Copyright © 2021 da/net research group <danet.fbi.h-da.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// pathgenCmd represents the pathgen command
var pathgenCmd = &cobra.Command{
Use: "pathgen",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("pathgen called")
},
}
func init() {
ygotCmd.AddCommand(pathgenCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// pathgenCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// pathgenCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
/*
Copyright © 2021 da/net research group <danet.fbi.h-da.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// protomapCmd represents the protomap command
var protomapCmd = &cobra.Command{
Use: "protomap",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("protomap called")
},
}
func init() {
ygotCmd.AddCommand(protomapCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// protomapCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// protomapCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
/*
Copyright © 2021 da/net research group <danet.fbi.h-da.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package cmd
import (
"code.fbi.h-da.de/cocsn/gosdn/cli"
"fmt"
"os"
"github.com/spf13/cobra"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "gosdn",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
RunE: func(cmd *cobra.Command, args []string) error {
return cli.Gosdn()
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.gosdn.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name ".gosdn" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".gosdn")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment