logoutSession not exportable

This commit is contained in:
Richard Nordström 2024-09-23 20:37:53 +02:00
parent d037ec32a1
commit bb35ca090f
No known key found for this signature in database
GPG Key ID: ACCB352EC60AF27C
2 changed files with 34 additions and 34 deletions

View File

@ -15,43 +15,43 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type LogoutSession struct { type logoutSession struct {
Profile string profile string
File config.File file config.File
PersistentAuth *auth.PersistentAuth persistentAuth *auth.PersistentAuth
} }
func (l *LogoutSession) load(ctx context.Context, profileName string, persistentAuth *auth.PersistentAuth) error { func (l *logoutSession) load(ctx context.Context, profileName string, persistentAuth *auth.PersistentAuth) error {
l.Profile = profileName l.profile = profileName
l.PersistentAuth = persistentAuth l.persistentAuth = persistentAuth
iniFile, err := profile.DefaultProfiler.Get(ctx) iniFile, err := profile.DefaultProfiler.Get(ctx)
if errors.Is(err, fs.ErrNotExist) { if errors.Is(err, fs.ErrNotExist) {
return err return err
} else if err != nil { } else if err != nil {
return fmt.Errorf("cannot parse config file: %w", err) return fmt.Errorf("cannot parse config file: %w", err)
} }
l.File = *iniFile l.file = *iniFile
if err := l.setHostAndAccountIdFromProfile(); err != nil { if err := l.setHostAndAccountIdFromProfile(); err != nil {
return err return err
} }
return nil return nil
} }
func (l *LogoutSession) setHostAndAccountIdFromProfile() error { func (l *logoutSession) setHostAndAccountIdFromProfile() error {
sectionMap, err := l.getConfigSectionMap() sectionMap, err := l.getConfigSectionMap()
if err != nil { if err != nil {
return err return err
} }
if sectionMap["host"] == "" { if sectionMap["host"] == "" {
return fmt.Errorf("no host configured for profile %s", l.Profile) return fmt.Errorf("no host configured for profile %s", l.profile)
} }
l.PersistentAuth.Host = sectionMap["host"] l.persistentAuth.Host = sectionMap["host"]
l.PersistentAuth.AccountID = sectionMap["account_id"] l.persistentAuth.AccountID = sectionMap["account_id"]
return nil return nil
} }
func (l *LogoutSession) getConfigSectionMap() (map[string]string, error) { func (l *logoutSession) getConfigSectionMap() (map[string]string, error) {
section, err := l.File.GetSection(l.Profile) section, err := l.file.GetSection(l.profile)
if err != nil { if err != nil {
return map[string]string{}, fmt.Errorf("profile does not exist in config file: %w", err) return map[string]string{}, fmt.Errorf("profile does not exist in config file: %w", err)
} }
@ -59,16 +59,16 @@ func (l *LogoutSession) getConfigSectionMap() (map[string]string, error) {
} }
// clear token from ~/.databricks/token-cache.json // clear token from ~/.databricks/token-cache.json
func (l *LogoutSession) clearTokenCache(ctx context.Context) error { func (l *logoutSession) clearTokenCache(ctx context.Context) error {
return l.PersistentAuth.ClearToken(ctx) return l.persistentAuth.ClearToken(ctx)
} }
// Overrewrite profile to .databrickscfg without fields marked as sensitive // Overrewrite profile to .databrickscfg without fields marked as sensitive
// Other attributes are preserved. // Other attributes are preserved.
func (l *LogoutSession) clearConfigFile(ctx context.Context, sectionMap map[string]string) error { func (l *logoutSession) clearConfigFile(ctx context.Context, sectionMap map[string]string) error {
return databrickscfg.SaveToProfile(ctx, &config.Config{ return databrickscfg.SaveToProfile(ctx, &config.Config{
ConfigFile: l.File.Path(), ConfigFile: l.file.Path(),
Profile: l.Profile, Profile: l.profile,
Host: sectionMap["host"], Host: sectionMap["host"],
ClusterID: sectionMap["cluster_id"], ClusterID: sectionMap["cluster_id"],
WarehouseID: sectionMap["warehouse_id"], WarehouseID: sectionMap["warehouse_id"],
@ -114,7 +114,7 @@ func newLogoutCommand(persistentAuth *auth.PersistentAuth) *cobra.Command {
} }
} }
defer persistentAuth.Close() defer persistentAuth.Close()
logoutSession := &LogoutSession{} logoutSession := &logoutSession{}
logoutSession.load(ctx, profileName, persistentAuth) logoutSession.load(ctx, profileName, persistentAuth)
configSectionMap, err := logoutSession.getConfigSectionMap() configSectionMap, err := logoutSession.getConfigSectionMap()
if err != nil { if err != nil {

View File

@ -26,11 +26,11 @@ func TestLogout_ClearConfigFile(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
iniFile, err := config.LoadFile(path) iniFile, err := config.LoadFile(path)
require.NoError(t, err) require.NoError(t, err)
logout := &LogoutSession{ logout := &logoutSession{
Profile: "abc", profile: "abc",
File: *iniFile, file: *iniFile,
} }
section, err := logout.File.GetSection("abc") section, err := logout.file.GetSection("abc")
assert.NoError(t, err) assert.NoError(t, err)
sectionMap := section.KeysHash() sectionMap := section.KeysHash()
err = logout.clearConfigFile(ctx, sectionMap) err = logout.clearConfigFile(ctx, sectionMap)
@ -62,15 +62,15 @@ func TestLogout_setHostAndAccountIdFromProfile(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
iniFile, err := config.LoadFile(path) iniFile, err := config.LoadFile(path)
require.NoError(t, err) require.NoError(t, err)
logout := &LogoutSession{ logout := &logoutSession{
Profile: "abc", profile: "abc",
File: *iniFile, file: *iniFile,
PersistentAuth: &auth.PersistentAuth{}, persistentAuth: &auth.PersistentAuth{},
} }
err = logout.setHostAndAccountIdFromProfile() err = logout.setHostAndAccountIdFromProfile()
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, logout.PersistentAuth.Host, "https://foo") assert.Equal(t, logout.persistentAuth.Host, "https://foo")
assert.Empty(t, logout.PersistentAuth.AccountID) assert.Empty(t, logout.persistentAuth.AccountID)
} }
func TestLogout_getConfigSectionMap(t *testing.T) { func TestLogout_getConfigSectionMap(t *testing.T) {
@ -86,10 +86,10 @@ func TestLogout_getConfigSectionMap(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
iniFile, err := config.LoadFile(path) iniFile, err := config.LoadFile(path)
require.NoError(t, err) require.NoError(t, err)
logout := &LogoutSession{ logout := &logoutSession{
Profile: "abc", profile: "abc",
File: *iniFile, file: *iniFile,
PersistentAuth: &auth.PersistentAuth{}, persistentAuth: &auth.PersistentAuth{},
} }
configSectionMap, err := logout.getConfigSectionMap() configSectionMap, err := logout.getConfigSectionMap()
assert.NoError(t, err) assert.NoError(t, err)