Skip to content

Commit

Permalink
Add helper method to config.Secret to load a file
Browse files Browse the repository at this point in the history
LoadFromFile is a very small helper to avoid repeating this code over
and over again.

This is related to prometheus/prometheus#8551

Signed-off-by: Marcelo E. Magallon <[email protected]>
  • Loading branch information
mem committed Aug 2, 2021
1 parent 8d1c9f8 commit cebfad9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package config

import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
)

Expand Down Expand Up @@ -48,6 +50,15 @@ func (s Secret) MarshalJSON() ([]byte, error) {
return json.Marshal(secretToken)
}

func (s *Secret) LoadFromFile(filename string) error {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return fmt.Errorf("cannot read %s: %w", filename, err)
}
*s = Secret(buf)
return nil
}

// DirectorySetter is a config type that contains file paths that may
// be relative to the file containing the config.
type DirectorySetter interface {
Expand Down
54 changes: 54 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package config

import (
"encoding/json"
"io/ioutil"
"os"
"testing"
)

Expand Down Expand Up @@ -53,3 +55,55 @@ func TestJSONMarshalSecret(t *testing.T) {
})
}
}

func TestSecretLoadFromFile(t *testing.T) {
dn, err := ioutil.TempDir("", "test-secret-loadfromfile.")
if err != nil {
t.Fatalf("cannot create temporary directory: %s", err)
}
defer os.RemoveAll(dn)

fh, err := ioutil.TempFile(dn, "")
if err != nil {
t.Fatalf("cannot create temporary file: %s", err)
}

fn := fh.Name()

secretData := "test"

n, err := fh.WriteString(secretData)
if err != nil {
t.Fatalf("cannot write to temporary file %s: %s", fn, err)
}

if n != len(secretData) {
t.Fatalf("short write writing to temporary file %s, expecting %d, got %d", fn, len(secretData), n)
}

err = fh.Close()
if err != nil {
t.Fatalf("error closing temporary file %s after write: %s", fn, err)
}

var s Secret
err = s.LoadFromFile(fn)
if err != nil {
t.Fatalf("cannot read secret from temporary file %s: %s", fn, err)
}

if string(s) != secretData {
t.Fatalf("unexpected secret data, expected %q, actual %q", secretData, string(s))
}

err = os.Remove(fn)
if err != nil {
t.Fatalf("cannot remove temporary file %s: %s", fn, err)
}

// this should report an error now
err = s.LoadFromFile(fn)
if err == nil {
t.Fatalf("expecting error reading non-existent temporary file %s, got nil", fn)
}
}

0 comments on commit cebfad9

Please sign in to comment.