github.com/christoph-karpowicz/db_mediator@v0.0.0-20210207102849-61a28a1071d8/internal/client/application/application.go (about) 1 package application 2 3 import ( 4 "io/ioutil" 5 "log" 6 "net/http" 7 "os" 8 "strconv" 9 "time" 10 11 "github.com/urfave/cli/v2" 12 ) 13 14 // Application is the main struct of the app. 15 type Application struct { 16 CLI *cli.App 17 client http.Client 18 simulate string 19 } 20 21 // Init initializes the client side app. 22 func (a *Application) Init() { 23 timeout := time.Duration(5 * time.Second) 24 a.client = http.Client{Timeout: timeout} 25 26 a.setCLI() 27 28 err := a.CLI.Run(os.Args) 29 if err != nil { 30 log.Fatal(err) 31 } 32 } 33 34 // setCLI configures the app's command line interface. 35 func (a *Application) setCLI() { 36 a.CLI = cli.NewApp() 37 a.CLI.UseShortOptionHandling = true 38 39 a.CLI.Name = "Database mediator CLI" 40 a.CLI.Usage = "Database synchronization app." 41 author := &cli.Author{Name: "Krzysztof Karpowicz", Email: "christoph.karpowicz@gmail.com"} 42 a.CLI.Authors = append(a.CLI.Authors, author) 43 a.CLI.Version = "1.0.0" 44 45 a.CLI.Commands = []*cli.Command{ 46 { 47 Name: "run", 48 Usage: "Start the specified synchronization.", 49 Flags: []cli.Flag{ 50 &cli.BoolFlag{ 51 Name: "simulate", 52 Aliases: []string{"s"}, 53 Usage: "Simulate a synchronization and show what changes would be made.", 54 }, 55 &cli.StringFlag{ 56 Name: "type", 57 Aliases: []string{"t"}, 58 Usage: "Specify the type of synchronization.", 59 }, 60 }, 61 Action: func(c *cli.Context) error { 62 var synchType string 63 synchTypeFlag := c.String("t") 64 simulateFlag := c.Bool("simulate") 65 66 switch true { 67 case synchTypeFlag == "" || synchTypeFlag == "oo" || synchTypeFlag == "one-off": 68 synchType = "one-off" 69 case synchTypeFlag == "ng" || synchTypeFlag == "ongoing": 70 synchType = "ongoing" 71 default: 72 log.Fatalln("ERROR: unknown synchronization type: " + synchTypeFlag + ".") 73 } 74 75 a.runSynch(synchType, c.Args().Get(0), simulateFlag) 76 77 return nil 78 }, 79 }, 80 { 81 Name: "stop", 82 Usage: "Stop the specified synchronization.", 83 // Flags: []cli.Flag{ 84 // &cli.BoolFlag{ 85 // Name: "all", 86 // Aliases: []string{"a"}, 87 // Usage: "Stop all synchronizations.", 88 // }, 89 // }, 90 Action: func(c *cli.Context) error { 91 // allFlag := c.Bool("all") 92 93 a.stopSynch(c.Args().Get(0)) 94 95 return nil 96 }, 97 }, 98 } 99 } 100 101 // runSynch prepares the parameters for a synchronization run request and invokes a GET function. 102 func (a *Application) runSynch(synchType string, synchName string, simulation bool) { 103 paramMap := make(map[string]string) 104 paramMap["type"] = synchType 105 paramMap["run"] = synchName 106 paramMap["simulation"] = strconv.FormatBool(simulation) 107 108 response := a.makeGETRequest("http://localhost:8000/runSynch", paramMap) 109 110 printRunResponse(response, synchType) 111 } 112 113 // stopSynch prepares the parameters for a synchronization stop request and invokes a GET function. 114 func (a *Application) stopSynch(synchName string) { 115 paramMap := make(map[string]string) 116 paramMap["stop"] = synchName 117 // paramMap["all"] = strconv.FormatBool(all) 118 119 response := a.makeGETRequest("http://localhost:8000/stopSynch", paramMap) 120 121 printStopResponse(response) 122 } 123 124 func (a *Application) makeGETRequest(url string, params map[string]string) map[string]interface{} { 125 req, err := http.NewRequest("GET", url, nil) 126 if err != nil { 127 log.Print(err) 128 os.Exit(1) 129 } 130 131 q := req.URL.Query() 132 for param, val := range params { 133 q.Add(param, val) 134 } 135 req.URL.RawQuery = q.Encode() 136 137 res, err := a.client.Do(req) 138 if err != nil { 139 log.Fatalln(err) 140 } 141 142 defer res.Body.Close() 143 144 resBody, err := ioutil.ReadAll(res.Body) 145 if err != nil { 146 log.Fatalln(err) 147 } 148 149 return parseResponse(resBody) 150 }