-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from tbxark-archive/master
feat: 读取本地fir token配置
- Loading branch information
Showing
3 changed files
with
85 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func LoadLocalToken() string { | ||
config, err := LoadLocalConfig() | ||
if err != nil { | ||
return "" | ||
} | ||
return config["token"] | ||
} | ||
|
||
func LoadLocalConfig() (map[string]string, error) { | ||
home, err := os.UserHomeDir() | ||
if err != nil { | ||
return nil, err | ||
} | ||
tokenPath := filepath.Join(home, ".fir-cli") | ||
raw, err := os.ReadFile(tokenPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if strings.HasPrefix(string(raw), "---\n") { | ||
raw = raw[4:] | ||
} | ||
data := make(map[string]string) | ||
for _, item := range strings.Split(string(raw), "\n") { | ||
cmp := strings.SplitN(item, ": ", 2) | ||
if len(cmp) == 2 && strings.HasPrefix(cmp[0], ":") { | ||
cmp[0] = cmp[0][1:len(cmp[0])] | ||
data[cmp[0]] = cmp[1] | ||
} | ||
} | ||
return data, nil | ||
} | ||
|
||
func SaveToLocal(email string, token string) error { | ||
raw := fmt.Sprintf("---\n:email: %s\n:token: %s\n", email, token) | ||
home, err := os.UserHomeDir() | ||
if err != nil { | ||
return err | ||
} | ||
tokenPath := filepath.Join(home, ".fir-cli") | ||
return os.WriteFile(tokenPath, []byte(raw), 0644) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestLoadLocalToken(t *testing.T) { | ||
for i := 0; i < 2; i++ { | ||
data, err := LoadLocalConfig() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
fmt.Println(data) | ||
err = SaveToLocal(data["email"], data["token"]) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
} |