Add --default-profile command-line flag which will create the profile if necessary and then use it

This commit is contained in:
Alessio 2024-02-21 19:53:12 -08:00
parent 688b64aee3
commit a5f1e8396e
3 changed files with 68 additions and 14 deletions

View File

@ -26,7 +26,7 @@ pipeline:
- branch: release-*
commands:
- cd cmd
# - ./tests.sh
- ./tests.sh
webserver_test:
image: offline-twitter/go
@ -38,14 +38,6 @@ pipeline:
- curl localhost:1000/cernovich > webserver_test_output.html
- jobs -p | xargs -I{} kill -- -{}
windows_build:
image: offline-twitter/go-mingw
commands:
- cd cmd
# - export version=$(echo $CI_COMMIT_BRANCH | grep -Poh "(?<=^release-)\d+\.\d+\.\d+")
- export version=60.43.53
- ./windows-compile.sh $version
version_bump_test:
image: offline-twitter/go
when:
@ -79,11 +71,21 @@ pipeline:
- scp offline-twitter-engine_$${version}_all.deb aptrepo@apt.playfulpachyderm.com:/apt-repo/test-repo
- ssh aptrepo@apt.playfulpachyderm.com "cd ~/test-repo && ./update.sh"
windows_build:
image: offline-twitter/go-mingw
when:
branch: release-*
commands:
- cd cmd
- export version=$(echo $CI_COMMIT_BRANCH | grep -Poh "(?<=^release-)\d+\.\d+\.\d+")
- ./windows-compile.sh $version
windows_package:
when:
branch: release-*
image: offline-twitter/innosetup
commands:
# - export version=$(echo $CI_COMMIT_BRANCH | grep -Poh "(?<=^release-)\d+\.\d+\.\d+")
- export version=60.43.53
- export version=$(echo $CI_COMMIT_BRANCH | grep -Poh "(?<=^release-)\d+\.\d+\.\d+")
- iscc.sh /Dversion=$version /Dexe_path=`winepath -w cmd/twitter.exe` build/windows/setup.iss

View File

@ -4,6 +4,8 @@ import (
_ "embed"
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper"
@ -56,3 +58,27 @@ func extract_id_from(url string) (scraper.TweetID, error) {
num, err := strconv.Atoi(url)
return scraper.TweetID(num), err
}
// Get a sensible default path to create a default profile. Uses `XDG_DATA_HOME` if available
//
// Defaults:
// - Unix: `~/.local/share`
// - Windows: %APPDATA%
// - MacOS: ~/Library
func get_default_profile() string {
app_data_dir := os.Getenv("XDG_DATA_HOME")
if app_data_dir == "" {
switch runtime.GOOS {
case "windows":
app_data_dir = os.Getenv("AppData")
if app_data_dir == "" {
panic("%AppData% is undefined")
}
case "darwin":
app_data_dir = filepath.Join(os.Getenv("HOME"), "Library")
default: // Unix
app_data_dir = filepath.Join(os.Getenv("HOME"), ".local", "share")
}
}
return filepath.Join(app_data_dir, "twitter")
}

View File

@ -5,6 +5,8 @@ import (
"fmt"
log "github.com/sirupsen/logrus"
"golang.org/x/term"
"errors"
"io/fs"
"os"
"strconv"
"strings"
@ -24,6 +26,8 @@ func main() {
profile_dir := flag.String("profile", ".", "")
flag.StringVar(profile_dir, "p", ".", "")
use_default_profile := flag.Bool("default-profile", false, "")
show_version_flag := flag.Bool("version", false, "")
flag.BoolVar(show_version_flag, "v", false, "")
@ -88,9 +92,33 @@ func main() {
return
}
if *use_default_profile {
if *profile_dir != "." {
die(fmt.Sprintf("Invalid flags: either `--profile [...]` or `--default-profile` can be used, but not both"), true, 2)
}
*profile_dir = get_default_profile()
// Create default profile if necessary
fileinfo, err := os.Stat(*profile_dir)
if errors.Is(err, fs.ErrNotExist) {
// Doesn't exist; create it
create_profile(*profile_dir)
} else if err != nil {
// Unexpected error
die(fmt.Sprintf("Default profile path (%s) is weird: %s", *profile_dir, err.Error()), false, 2)
} else if !fileinfo.IsDir() {
// It exists but it's not a directory
die(fmt.Sprintf("Default profile path (%s) already exists and is not a directory", *profile_dir), false, 2)
}
// Path exists and is a directory; safe to continue
}
profile, err = persistence.LoadProfile(*profile_dir)
if err != nil {
die(fmt.Sprintf("Could not load profile: %s", err.Error()), true, 2)
if *use_default_profile {
create_profile(*profile_dir)
} else {
die(fmt.Sprintf("Could not load profile: %s", err.Error()), true, 2)
}
}
if *session_name != "" {
@ -105,8 +133,6 @@ func main() {
}
switch operation {
case "create_profile":
create_profile(target)
case "login":
var password string
if len(args) == 2 {