1//go:build fakekeyring23package keyring45import (6 "context"7 "errors"8 "os"9 "path/filepath"10 "strings"11)1213// System, in binaries built with -tags fakekeyring (the e2e tests), is a14// file in $XDG_CONFIG_HOME/012, which the tests point at a temporary15// directory, so they never reach a real credential store.16func System() Store { return fileStore{} }1718type fileStore struct{}1920func (fileStore) path() (string, error) {21 x := os.Getenv("XDG_CONFIG_HOME")22 if x == "" {23 return "", errors.New("the test credential store needs XDG_CONFIG_HOME")24 }25 return filepath.Join(x, "012", "test-credential-store"), nil26}2728func (fileStore) Name() string { return "test credential store" }2930func (f fileStore) Get(context.Context) (string, error) {31 p, err := f.path()32 if err != nil {33 return "", err34 }35 b, err := os.ReadFile(p)36 if errors.Is(err, os.ErrNotExist) {37 return "", ErrNotFound38 }39 return strings.TrimSpace(string(b)), err40}4142func (f fileStore) Set(_ context.Context, secret string) error {43 p, err := f.path()44 if err != nil {45 return err46 }47 if err := os.MkdirAll(filepath.Dir(p), 0o700); err != nil {48 return err49 }50 return os.WriteFile(p, []byte(secret), 0o600)51}5253func (f fileStore) Delete(context.Context) error {54 p, err := f.path()55 if err != nil {56 return err57 }58 if err := os.Remove(p); errors.Is(err, os.ErrNotExist) {59 return ErrNotFound60 } else if err != nil {61 return err62 }63 return nil64}