Skip to content

Commit

Permalink
Fix README example code
Browse files Browse the repository at this point in the history
  • Loading branch information
sa6mwa committed Mar 14, 2023
1 parent 3c9e261 commit 97d57a6
Showing 1 changed file with 57 additions and 46 deletions.
103 changes: 57 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,71 @@ the application or provide one via environment variables, using `Stash`,
`Unstash` and `EditThing` is simple...

```go
package main

import (
"encoding/json"
"fmt"
"log"
"os"

"github.com/sa6mwa/anystore"
)

// All fields need to be exported.
type MyConfig struct {
ListenAddress string
Username string
Token string
Endpoints []*Endpoint
ListenAddress string
Username string
Token string
Endpoints []*Endpoint
}

type Endpoint struct {
ID int
Name string
URL string
ID int
Name string
URL string
}

func main() {
defaultConf := &MyConfig{
ListenAddress: "0.0.0.0:1234",
Username: "superuser",
Token: "abc123",
Endpoints: []*Endpoint{
{ID: 1, Name: "Endpoint 1", URL: "https://endpoint1.local"},
{ID: 2, Name: "Endpoint 2", URL: "https://endpoint2.local"},
}
}
file := "~/.myconfigfile.db"

var configuration MyConfig

if err := anystore.Unstash(&anystore.StashConfig{
File: file,
EncryptionKey: anystore.DefaultEncryptionKey,
Key: "configuration",
Thing: &configuration,
}, defaultConf); err != nil {
log.Fatal(err)
}

if len(os.Args) > 1 && os.Args[1] == "edit" {
if err := anystore.EditThing(&anystore.StashConfig{
File: file,
EncryptionKey: anystore.DefaultEncryptionKey,
Key: "configuration",
Thing: &configuration,
// Editor: "/usr/bin/emacs",
}, defaultConf); err != nil {
log.Fatal(err)
}
}

j, err := json.MarshalIndent(configuration, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(j))
defaultConf := &MyConfig{
ListenAddress: "0.0.0.0:1234",
Username: "superuser",
Token: "abc123",
Endpoints: []*Endpoint{
{ID: 1, Name: "Endpoint 1", URL: "https://endpoint1.local"},
{ID: 2, Name: "Endpoint 2", URL: "https://endpoint2.local"},
},
}
file := "~/.myconfigfile.db"

var configuration MyConfig

if err := anystore.Unstash(&anystore.StashConfig{
File: file,
EncryptionKey: anystore.DefaultEncryptionKey,
Key: "configuration",
Thing: &configuration,
}, defaultConf); err != nil {
log.Fatal(err)
}

if len(os.Args) > 1 && os.Args[1] == "edit" {
if err := anystore.EditThing(&anystore.StashConfig{
File: file,
EncryptionKey: anystore.DefaultEncryptionKey,
Key: "configuration",
Thing: &configuration,
// Editor: "/usr/bin/emacs",
}, defaultConf); err != nil {
log.Fatal(err)
}
}

j, err := json.MarshalIndent(configuration, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(j))
}
```

Expand Down

0 comments on commit 97d57a6

Please sign in to comment.