github.com/martinohmann/rfoutlet@v1.2.1-0.20220707195255-8a66aa411105/main.go (about)

     1  // rfoutlet provides outlet control via cli and web interface for
     2  // Raspberry PI 2/3.
     3  //
     4  // The transmitter and receiver logic has been ported from the great
     5  // https://github.com/sui77/rc-switch C++ project to golang.
     6  //
     7  // rfoutlet comes with ready to use commands for transmitting and receiving
     8  // remote control codes as well as a command for serving a web interface (see
     9  // cmd/ directory). The pkg/ directory exposes the gpio package which contains
    10  // the receiver and transmitter code.
    11  package main
    12  
    13  import (
    14  	"fmt"
    15  	"path"
    16  	"runtime"
    17  
    18  	"github.com/gin-gonic/gin"
    19  	"github.com/martinohmann/rfoutlet/cmd"
    20  	log "github.com/sirupsen/logrus"
    21  	"github.com/spf13/cobra"
    22  )
    23  
    24  var debug bool
    25  
    26  func newRootCommand() *cobra.Command {
    27  	cmd := &cobra.Command{
    28  		Use:           "rfoutlet",
    29  		Short:         "A tool for interacting with remote controlled outlets",
    30  		Long:          "rfoutlet is a tool for interacting with remote controlled outlets. It provides functionality to sniff and transmit the codes controlling the outlets.",
    31  		SilenceUsage:  true,
    32  		SilenceErrors: true,
    33  		PersistentPreRun: func(cmd *cobra.Command, args []string) {
    34  			gin.SetMode(gin.ReleaseMode)
    35  
    36  			if debug {
    37  				gin.SetMode(gin.DebugMode)
    38  				log.SetLevel(log.DebugLevel)
    39  				log.SetFormatter(&log.TextFormatter{
    40  					FullTimestamp: true,
    41  					CallerPrettyfier: func(f *runtime.Frame) (string, string) {
    42  						return f.Function, fmt.Sprintf("%s:%d", path.Base(f.File), f.Line)
    43  					},
    44  				})
    45  				log.SetReportCaller(true)
    46  			}
    47  		},
    48  	}
    49  
    50  	cmd.PersistentFlags().BoolVar(&debug, "debug", debug, "enable debug mode. this will cause more verbose output")
    51  	cmd.PersistentFlags().String("gpio-chip", "gpiochip0", "name of the GPIO chip to interact with")
    52  	cmd.PersistentFlags().Bool("gpio-mockup", false, "automatically load and unload the gpio-mockup kernel module, useful for testing")
    53  
    54  	return cmd
    55  }
    56  
    57  func main() {
    58  	rootCmd := newRootCommand()
    59  
    60  	rootCmd.AddCommand(cmd.NewServeCommand())
    61  	rootCmd.AddCommand(cmd.NewSniffCommand())
    62  	rootCmd.AddCommand(cmd.NewTransmitCommand())
    63  
    64  	if err := rootCmd.Execute(); err != nil {
    65  		log.Fatal(err)
    66  	}
    67  }