cmd

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2024 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

cmd pkg handles cobra api for command line tool

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:     "vd",
	Args:    cobra.ExactArgs(1),
	Version: longVersion,
	Short:   "vd is a easy to use device simulator",
	Long: `Virtual Device is an open source program that can be used to simulate lab device communication streams. 
It is useful for testing and debugging software that communicates with lab devices, 
as well as for creating virtual lab environments for education and research.
To run simulator create .toml file with device description and run it using following commands:

	vd vdfile.toml
	vd vdfile.toml --listenAddr 127.0.0.1:6666

By default, vd is listenning on 127.0.0.1:9999.`,
	Run: func(cmd *cobra.Command, args []string) {

		fmt.Printf(banner, version, website)

		vdfile, err := vdfile.ReadVDFile(args[0])
		if err != nil {
			fmt.Printf("Config loading failed %v", err)
			os.Exit(1)
		}

		ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
		defer stop()

		str, err := device.NewDevice(vdfile)
		if err != nil {
			fmt.Printf("Device creation failed %v", err)
			os.Exit(1)
		}

		ip := viper.GetString("listenAddr")
		if !verifyIPAddr(ip) {
			fmt.Println("Wrong TCP address")
			os.Exit(1)
		}

		srv, err := server.New(str, ip)
		if err != nil {
			fmt.Printf("TCP server creation failed %v", err)
			os.Exit(1)
		}

		go srv.Start()
		fmt.Println("vd running on ", gchalk.BrightYellow(ip))

		addr := viper.GetString("httpListenAddr")
		if !verifyIPAddr(addr) {
			fmt.Println("Wrong HTTP address")
			os.Exit(1)
		}

		a := api.NewHttpApiServer(str)

		go func() {

			err = a.Serve(ctx, addr)
			if err != nil {
				fmt.Printf("HTTP server failed %v", err)
				os.Exit(1)
			}
		}()

		<-ctx.Done()
		srv.Stop()
		fmt.Println("vd stopped")
	},
}

rootCmd represents the base command when called without any subcommands

Functions

func Execute

func Execute(f fs.FS)

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. This is the main method that start Cobra CLI.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL