Create config file when it's missing
This commit is contained in:
		
							parent
							
								
									a6b95f50c9
								
							
						
					
					
						commit
						78cf8d6ca2
					
				@ -3,7 +3,6 @@ package config
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"io/ioutil"
 | 
						"io/ioutil"
 | 
				
			||||||
	"path/filepath"
 | 
					 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/ooni/probe-cli/utils"
 | 
						"github.com/ooni/probe-cli/utils"
 | 
				
			||||||
@ -14,7 +13,7 @@ import (
 | 
				
			|||||||
func ReadConfig(path string) (*Config, error) {
 | 
					func ReadConfig(path string) (*Config, error) {
 | 
				
			||||||
	b, err := ioutil.ReadFile(path)
 | 
						b, err := ioutil.ReadFile(path)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, errors.Wrap(err, "reading file")
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	c, err := ParseConfig(b)
 | 
						c, err := ParseConfig(b)
 | 
				
			||||||
@ -93,7 +92,7 @@ func (c *Config) Default() error {
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	c.path = filepath.Join(home, "config.json")
 | 
						c.path = utils.ConfigPath(home)
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										41
									
								
								ooni.go
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								ooni.go
									
									
									
									
									
								
							@ -4,11 +4,11 @@ import (
 | 
				
			|||||||
	"io/ioutil"
 | 
						"io/ioutil"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path"
 | 
						"path"
 | 
				
			||||||
	"path/filepath"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/apex/log"
 | 
						"github.com/apex/log"
 | 
				
			||||||
	"github.com/jmoiron/sqlx"
 | 
						"github.com/jmoiron/sqlx"
 | 
				
			||||||
	"github.com/ooni/probe-cli/config"
 | 
						"github.com/ooni/probe-cli/config"
 | 
				
			||||||
 | 
						"github.com/ooni/probe-cli/internal/bindata"
 | 
				
			||||||
	"github.com/ooni/probe-cli/internal/database"
 | 
						"github.com/ooni/probe-cli/internal/database"
 | 
				
			||||||
	"github.com/ooni/probe-cli/internal/legacy"
 | 
						"github.com/ooni/probe-cli/internal/legacy"
 | 
				
			||||||
	"github.com/ooni/probe-cli/internal/onboard"
 | 
						"github.com/ooni/probe-cli/internal/onboard"
 | 
				
			||||||
@ -100,7 +100,7 @@ func (c *Context) Init() error {
 | 
				
			|||||||
		c.Config, err = config.ReadConfig(c.configPath)
 | 
							c.Config, err = config.ReadConfig(c.configPath)
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		log.Debug("Reading default config file")
 | 
							log.Debug("Reading default config file")
 | 
				
			||||||
		c.Config, err = ReadDefaultConfigPaths(c.Home)
 | 
							c.Config, err = InitDefaultConfig(c.Home)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
@ -144,21 +144,36 @@ func MaybeInitializeHome(home string) error {
 | 
				
			|||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ReadDefaultConfigPaths from common locations.
 | 
					// InitDefaultConfig reads the config from common locations or creates it if
 | 
				
			||||||
func ReadDefaultConfigPaths(home string) (*config.Config, error) {
 | 
					// missing.
 | 
				
			||||||
	var paths = []string{
 | 
					func InitDefaultConfig(home string) (*config.Config, error) {
 | 
				
			||||||
		filepath.Join(home, "config.json"),
 | 
						var (
 | 
				
			||||||
	}
 | 
							err        error
 | 
				
			||||||
	for _, path := range paths {
 | 
							c          *config.Config
 | 
				
			||||||
		if _, err := os.Stat(path); err == nil {
 | 
							configPath = utils.ConfigPath(home)
 | 
				
			||||||
			c, err := config.ReadConfig(path)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						c, err = config.ReadConfig(configPath)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							if os.IsNotExist(err) {
 | 
				
			||||||
 | 
								log.Debugf("writing default config to %s", configPath)
 | 
				
			||||||
 | 
								var data []byte
 | 
				
			||||||
 | 
								data, err = bindata.Asset("data/default-config.json")
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return nil, err
 | 
									return nil, err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			return c, nil
 | 
								err = ioutil.WriteFile(
 | 
				
			||||||
 | 
									configPath,
 | 
				
			||||||
 | 
									data,
 | 
				
			||||||
 | 
									0644,
 | 
				
			||||||
 | 
								)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								return InitDefaultConfig(home)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Run from the default config
 | 
						return c, nil
 | 
				
			||||||
	return config.ReadConfig(paths[0])
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -20,6 +20,11 @@ func RequiredDirs(home string) []string {
 | 
				
			|||||||
	return requiredDirs
 | 
						return requiredDirs
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// ConfigPath returns the default path to the config file
 | 
				
			||||||
 | 
					func ConfigPath(home string) string {
 | 
				
			||||||
 | 
						return filepath.Join(home, "config.json")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// GeoIPDir returns the geoip data dir for the given OONI Home
 | 
					// GeoIPDir returns the geoip data dir for the given OONI Home
 | 
				
			||||||
func GeoIPDir(home string) string {
 | 
					func GeoIPDir(home string) string {
 | 
				
			||||||
	return filepath.Join(home, "geoip")
 | 
						return filepath.Join(home, "geoip")
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user