FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
03-23-2021 04:27 PM - edited 03-23-2021 04:28 PM
If my understanding is correct the solutions snap should be used to read and write configuration files. I'm trying to do this in Go, and have added the "active-solution" plug to my snapcraft.yml
plugs: active-solution: interface: content content: solutions target: $SNAP_COMMON/solutions
My code is checking if the directory and file exists, and creates them if not. Creating seems to work, but reading the file results in the following error:
Could not read configuration file: open /var/snap/rexroth-solutions/common/solutions/DefaultSolution/configurations/Default/my-snap/config.json: permission denied
Trying to view the created directory from the sidebar in the Configurations menu of the virtual control shows this error:
open /var/snap/rexroth-solutions/common/solutions/DefaultSolution/configurations/Default/my-snap: permission denied
This is the Go source code, any ideas what permissions need to be changed when creating the configuration directory and file?
type Config struct { Greeting string `json:"greeting"` } snapCommon := os.Getenv("SNAP_COMMON") activeConfiguration := path.Join(snapCommon, "/solutions/activeConfiguration") resolvedPath, err := os.Readlink(activeConfiguration) if err != nil { log.Fatalln("Could not resolve active configuration path: ", err) } configurationPath := path.Join(resolvedPath, "/my-snap") if _, err := os.Stat(configurationPath); os.IsNotExist(err) { os.Mkdir(configurationPath, os.ModeDir) } configurationFile := path.Join(configurationPath, "/config.json") if _, err := os.Stat(configurationFile); os.IsNotExist(err) { file, err := json.MarshalIndent(Config{Greeting: "Hello World"}, "", " ") if err != nil { log.Fatalln("Could not write default configuration file: ", err) } ioutil.WriteFile(configurationFile, file, 0644) } config := Config{} file, err := ioutil.ReadFile(configurationFile) if err != nil { log.Fatalln("Could not read configuration file: ", err) } err = json.Unmarshal([]byte(file), &config) if err != nil { log.Fatalln("Could not parse configuration file: ", err) }
Solved! Go to Solution.
04-09-2021 01:47 PM
hi jand,
I guess that the problems you are observing are related to the resolvedPath. os.Readlink obviously returns the absolute path to the rexroth-solutions snap, which can’t be accessed from outside.
As you are using this path as a base path for configurationPath and configurationFile, os.Mkdir and ioutil.WriteFile won’t probably work either (you just don’t check for errors here).
Instead of the resolvedPath, use the path you have already assigned to the activeConfiguration variable. This path addresses the rexroth-solutions snap through the solutions slot, which should work as expected.
hope this helps,
Andreas