func Config()

in calnex/config/config.go [161:216]


func Config(target string, insecureTLS bool, cc CalnexConfig, apply bool) error {
	var c config
	api := api.NewAPI(target, insecureTLS)

	f, err := api.FetchSettings()
	if err != nil {
		return err
	}

	s := f.Section("measure")

	// set static config
	c.baseConfig(s)

	// set measure config
	c.measureConfig(s, cc)

	if !apply {
		log.Infof("dry run. Exiting")
		return nil
	}

	// check measurement status
	status, err := api.FetchStatus()
	if err != nil {
		return err
	}

	if c.changed {
		if status.MeasurementActive {
			log.Infof("stopping measurement")
			// stop measurement
			if err = api.StopMeasure(); err != nil {
				return err
			}
		}

		log.Infof("pushing the config")
		// set the modified config
		if err = api.PushSettings(f); err != nil {
			return err
		}
	} else {
		log.Infof("no change needs to be applied")
	}

	if c.changed || !status.MeasurementActive {
		log.Infof("starting measurement")
		// start measurement
		if err = api.StartMeasure(); err != nil {
			return err
		}
	}

	return nil
}