Skip to content

Commit 6487a09

Browse files
committed
added file based login
1 parent f228b56 commit 6487a09

File tree

7 files changed

+75
-38
lines changed

7 files changed

+75
-38
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
bin/
22
dist/
33
**/basic.config.ts
44
**/basic.config.js

.goreleaser.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
project_name: basic-cli
1010
version: 2
1111

12-
env:
13-
- GITHUB_TOKEN={{ .Env.GITHUB_TOKEN }}
1412
before:
1513
hooks:
1614
# You may remove this if you don't use go modules.

dev.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
release notes:
2+
3+
update github tag - match with version in package.json
4+
5+
run goreleaser release --clean to create a new release and publish to github
6+
7+
publish to npm
8+
9+

index.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
const path = require('path');
3-
const { execFile } = require('child_process');
3+
const { execFile, execSync } = require('child_process');
44
const os = require('os');
55
const fs = require('fs');
66

@@ -30,21 +30,20 @@ if (!fs.existsSync(binary)) {
3030
process.exit(1);
3131
}
3232

33-
console.log(`Attempting to execute binary: ${binary}`);
34-
console.log(`Arguments: ${process.argv.slice(2).join(' ')}`);
3533

36-
execFile(binary, process.argv.slice(2), { encoding: 'utf8' }, (error, stdout, stderr) => {
37-
if (error) {
38-
console.error(`Error executing binary: ${error.message}`);
39-
console.error(`Command: ${binary} ${process.argv.slice(2).join(' ')}`);
40-
console.error(`Exit code: ${error.code}`);
41-
if (stderr) {
42-
console.error(`stderr: ${stderr}`);
43-
}
44-
if (stdout) {
45-
console.error(`stdout: ${stdout}`);
46-
}
47-
process.exit(1);
34+
const sourcePath = binary
35+
const destPath = path.join(__dirname, 'bin', 'basic-cli');
36+
37+
try {
38+
// Ensure the bin directory exists
39+
fs.mkdirSync(path.dirname(destPath), { recursive: true });
40+
41+
fs.copyFileSync(sourcePath, destPath);
42+
if (os.platform() !== 'win32') {
43+
execSync(`chmod +x ${destPath}`);
4844
}
49-
console.log(stdout);
50-
});
45+
console.log(`Installed the ${os.platform()} binary.`);
46+
} catch (error) {
47+
console.error('Error during post-install script:', error);
48+
process.exit(1);
49+
}

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
{
22
"name": "@basictech/cli",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "basictech cli for creating & managing your projects",
55
"main": "index.js",
6-
"bin": {
7-
"basic": "./index.js"
8-
},
96
"files": [
107
"dist",
118
"index.js"
129
],
10+
"bin": {
11+
"basic": "./bin/basic-cli"
12+
},
1313
"scripts": {
1414
"postinstall": "node ./index.js",
1515
"test": "echo \"Error: no test specified\" && exit 1"
1616
},
1717
"author": "raz@basic.tech",
18-
"license": "ISC"
18+
"license": "ISC",
19+
"dependencies": {}
1920
}

src/main.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"os"
1111
"os/exec"
12+
"path/filepath"
1213
"regexp"
1314
"runtime"
1415
"strings"
@@ -33,6 +34,12 @@ var (
3334
green = lipgloss.AdaptiveColor{Light: "#02BA84", Dark: "#02BF87"}
3435
)
3536

37+
// Update the constants for the directory and file name
38+
const (
39+
basicCliDirName = ".basic-cli"
40+
tokenFileName = "token.json"
41+
)
42+
3643
type Styles struct {
3744
Base,
3845
HeaderText,
@@ -470,7 +477,7 @@ func init() {
470477
func main() {
471478
if len(os.Args) < 2 {
472479
fmt.Println("Usage: basic <command> [arguments]")
473-
os.Exit(1)
480+
os.Exit(0)
474481
}
475482

476483
command := os.Args[1]
@@ -877,37 +884,55 @@ func getKeyring() (keyring.Keyring, error) {
877884
})
878885
}
879886

880-
func saveToken(token *oauth2.Token) error {
881-
ring, err := getKeyring()
887+
// Add a helper function to get the token file path
888+
func getTokenFilePath() (string, error) {
889+
homeDir, err := os.UserHomeDir()
882890
if err != nil {
883-
return err
891+
return "", err
884892
}
893+
basicCliDir := filepath.Join(homeDir, basicCliDirName)
894+
return filepath.Join(basicCliDir, tokenFileName), nil
895+
}
885896

897+
// Update the saveToken function
898+
func saveToken(token *oauth2.Token) error {
886899
customToken := &CustomToken{Token: *token}
887900
tokenJSON, err := json.Marshal(customToken)
888901
if err != nil {
889902
return err
890903
}
891904

892-
return ring.Set(keyring.Item{
893-
Key: tokenKey,
894-
Data: tokenJSON,
895-
})
905+
tokenFilePath, err := getTokenFilePath()
906+
if err != nil {
907+
return err
908+
}
909+
910+
// Create the .basic-cli directory if it doesn't exist
911+
dir := filepath.Dir(tokenFilePath)
912+
if err := os.MkdirAll(dir, 0700); err != nil {
913+
return err
914+
}
915+
916+
return os.WriteFile(tokenFilePath, tokenJSON, 0600)
896917
}
897918

919+
// Update the loadToken function
898920
func loadToken() (*oauth2.Token, error) {
899-
ring, err := getKeyring()
921+
tokenFilePath, err := getTokenFilePath()
900922
if err != nil {
901923
return nil, err
902924
}
903925

904-
item, err := ring.Get(tokenKey)
926+
tokenData, err := os.ReadFile(tokenFilePath)
905927
if err != nil {
928+
if os.IsNotExist(err) {
929+
return nil, nil // No token file exists
930+
}
906931
return nil, err
907932
}
908933

909934
var customToken CustomToken
910-
err = json.Unmarshal(item.Data, &customToken)
935+
err = json.Unmarshal(tokenData, &customToken)
911936
if err != nil {
912937
return nil, err
913938
}
@@ -931,13 +956,18 @@ func loadToken() (*oauth2.Token, error) {
931956
return &customToken.Token, nil
932957
}
933958

959+
// Update the deleteToken function
934960
func deleteToken() error {
935-
ring, err := getKeyring()
961+
tokenFilePath, err := getTokenFilePath()
936962
if err != nil {
937963
return err
938964
}
939965

940-
return ring.Remove(tokenKey)
966+
err = os.Remove(tokenFilePath)
967+
if err != nil && !os.IsNotExist(err) {
968+
return err
969+
}
970+
return nil
941971
}
942972

943973
func refreshToken(token *oauth2.Token) (*oauth2.Token, error) {

src/src

10.6 MB
Binary file not shown.

0 commit comments

Comments
 (0)