Store the host in the config file
This commit is contained in:
		
							parent
							
								
									a89e577aec
								
							
						
					
					
						commit
						832f206a53
					
				
					 1 changed files with 30 additions and 23 deletions
				
			
		|  | @ -30,19 +30,19 @@ var home = os.Getenv("HOME") | ||||||
| var filepath = path.Join(home, ".local/share/rove.json") | var filepath = path.Join(home, ".local/share/rove.json") | ||||||
| 
 | 
 | ||||||
| var host = flag.String("host", "api.rove-game.com", "path to game host server") | var host = flag.String("host", "api.rove-game.com", "path to game host server") | ||||||
| var dataPath = flag.String("data", filepath, "data file for storage") | var data = flag.String("data", filepath, "data file for storage") | ||||||
| 
 | 
 | ||||||
| // Data is used to store internal data | // Config is used to store internal data | ||||||
| type Data struct { | type Config struct { | ||||||
| 	Account string `json:"account,omitempty"` | 	Account string `json:"account,omitempty"` | ||||||
| 	Host    string `json:"host,omitempty"` | 	Host    string `json:"host,omitempty"` | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| var name = flag.String("name", "", "used with status command for the account name") | var name = flag.String("name", "", "used with status command for the account name") | ||||||
| 
 | 
 | ||||||
| func verifyId(d Data) { | func verifyId(d Config) { | ||||||
| 	if len(d.Account) == 0 { | 	if len(d.Account) == 0 { | ||||||
| 		fmt.Fprintf(os.Stderr, "No account ID set, must register first or set \"account\" value in %s\n", *dataPath) | 		fmt.Fprintf(os.Stderr, "No account ID set, must register first or set \"account\" value in %s\n", *data) | ||||||
| 		os.Exit(1) | 		os.Exit(1) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | @ -58,24 +58,31 @@ func main() { | ||||||
| 		os.Exit(1) | 		os.Exit(1) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	// Set up the config | ||||||
|  | 	var config = Config{ | ||||||
|  | 		Host: *host, | ||||||
|  | 	} | ||||||
|  | 	var server = rove.Server(*host) | ||||||
|  | 
 | ||||||
| 	// Load in the persistent file | 	// Load in the persistent file | ||||||
| 	var data = Data{} | 	_, err := os.Stat(*data) | ||||||
| 	_, err := os.Stat(*dataPath) |  | ||||||
| 	if !os.IsNotExist(err) { | 	if !os.IsNotExist(err) { | ||||||
| 		if b, err := ioutil.ReadFile(*dataPath); err != nil { | 		if b, err := ioutil.ReadFile(*data); err != nil { | ||||||
| 			fmt.Fprintf(os.Stderr, "failed to read file %s error: %s\n", *dataPath, err) | 			fmt.Fprintf(os.Stderr, "failed to read file %s error: %s\n", *data, err) | ||||||
| 			os.Exit(1) | 			os.Exit(1) | ||||||
| 		} else if len(b) == 0 { | 		} else if len(b) == 0 { | ||||||
| 			fmt.Fprintf(os.Stderr, "file %s was empty, assumin fresh data\n", *dataPath) | 			fmt.Fprintf(os.Stderr, "file %s was empty, assumin fresh data\n", *data) | ||||||
| 
 | 
 | ||||||
| 		} else if err := json.Unmarshal(b, &data); err != nil { | 		} else if err := json.Unmarshal(b, &config); err != nil { | ||||||
| 			fmt.Fprintf(os.Stderr, "failed to unmarshal file %s error: %s\n", *dataPath, err) | 			fmt.Fprintf(os.Stderr, "failed to unmarshal file %s error: %s\n", *data, err) | ||||||
| 			os.Exit(1) | 			os.Exit(1) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	var server = rove.Server(*host) | 	// Print the config info | ||||||
|  | 	fmt.Printf("host: %s\taccount: %s\n", config.Host, config.Account) | ||||||
| 
 | 
 | ||||||
|  | 	// Handle all the commands | ||||||
| 	command := args[0] | 	command := args[0] | ||||||
| 	switch command { | 	switch command { | ||||||
| 	case "status": | 	case "status": | ||||||
|  | @ -102,11 +109,11 @@ func main() { | ||||||
| 
 | 
 | ||||||
| 		} else { | 		} else { | ||||||
| 			fmt.Printf("Registered account with id: %s\n", response.Id) | 			fmt.Printf("Registered account with id: %s\n", response.Id) | ||||||
| 			data.Account = response.Id | 			config.Account = response.Id | ||||||
| 		} | 		} | ||||||
| 	case "spawn": | 	case "spawn": | ||||||
| 		verifyId(data) | 		verifyId(config) | ||||||
| 		d := rove.SpawnData{Id: data.Account} | 		d := rove.SpawnData{Id: config.Account} | ||||||
| 		if response, err := server.Spawn(d); err != nil { | 		if response, err := server.Spawn(d); err != nil { | ||||||
| 			fmt.Fprintln(os.Stderr, err) | 			fmt.Fprintln(os.Stderr, err) | ||||||
| 			os.Exit(1) | 			os.Exit(1) | ||||||
|  | @ -120,8 +127,8 @@ func main() { | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 	case "commands": | 	case "commands": | ||||||
| 		verifyId(data) | 		verifyId(config) | ||||||
| 		d := rove.CommandsData{Id: data.Account} | 		d := rove.CommandsData{Id: config.Account} | ||||||
| 
 | 
 | ||||||
| 		// TODO: Send real commands in | 		// TODO: Send real commands in | ||||||
| 
 | 
 | ||||||
|  | @ -139,8 +146,8 @@ func main() { | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 	case "radar": | 	case "radar": | ||||||
| 		verifyId(data) | 		verifyId(config) | ||||||
| 		d := rove.RadarData{Id: data.Account} | 		d := rove.RadarData{Id: config.Account} | ||||||
| 		if response, err := server.Radar(d); err != nil { | 		if response, err := server.Radar(d); err != nil { | ||||||
| 			fmt.Fprintln(os.Stderr, err) | 			fmt.Fprintln(os.Stderr, err) | ||||||
| 			os.Exit(1) | 			os.Exit(1) | ||||||
|  | @ -160,12 +167,12 @@ func main() { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// Save out the persistent file | 	// Save out the persistent file | ||||||
| 	if b, err := json.MarshalIndent(data, "", "\t"); err != nil { | 	if b, err := json.MarshalIndent(config, "", "\t"); err != nil { | ||||||
| 		fmt.Fprintf(os.Stderr, "failed to marshal data error: %s\n", err) | 		fmt.Fprintf(os.Stderr, "failed to marshal data error: %s\n", err) | ||||||
| 		os.Exit(1) | 		os.Exit(1) | ||||||
| 	} else { | 	} else { | ||||||
| 		if err := ioutil.WriteFile(*dataPath, b, os.ModePerm); err != nil { | 		if err := ioutil.WriteFile(*data, b, os.ModePerm); err != nil { | ||||||
| 			fmt.Fprintf(os.Stderr, "failed to save file %s error: %s\n", *dataPath, err) | 			fmt.Fprintf(os.Stderr, "failed to save file %s error: %s\n", *data, err) | ||||||
| 			os.Exit(1) | 			os.Exit(1) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue