From 3053c19dd0e0b3cc25ddfc0405ce740422c47930 Mon Sep 17 00:00:00 2001 From: ari melody Date: Sun, 28 Jun 2026 13:40:08 +0100 Subject: [PATCH] code refactor: tidy-up --- main.go | 90 ++-------------------------------------------- oauth/oauth.go | 73 +++++++++++++++++++++++++++++++++++++ scanner/scanner.go | 22 ++++++++++++ 3 files changed, 98 insertions(+), 87 deletions(-) create mode 100644 oauth/oauth.go diff --git a/main.go b/main.go index bf54ed9..ef109d8 100644 --- a/main.go +++ b/main.go @@ -7,17 +7,16 @@ import ( "fmt" "log" "math" - "net/http" "os" "path" "strings" - "sync" "golang.org/x/oauth2" "golang.org/x/oauth2/google" "google.golang.org/api/youtube/v3" "arimelody.space/vodular/config" + "arimelody.space/vodular/oauth" "arimelody.space/vodular/scanner" vid "arimelody.space/vodular/video" yt "arimelody.space/vodular/youtube" @@ -144,7 +143,7 @@ func main() { // initialising directory (--init) if initDirectory { - err = initialiseDirectory(directory) + err = scanner.InitialiseDirectory(directory) if err != nil { log.Fatalf("Failed to initialise directory: %v", err) os.Exit(1) @@ -321,7 +320,7 @@ func main() { if cfg.Token != nil { token = cfg.Token } else { - token, err = generateOAuthToken(&ctx, oauth2Config, cfg) + token, err = oauth.GenerateToken(&ctx, oauth2Config, cfg) if err != nil { log.Fatalf("OAuth flow failed: %v", err) os.Exit(1) @@ -374,86 +373,3 @@ func main() { log.Print("Dry run: Skipping video upload") } } - -func initialiseDirectory(directory string) error { - dirInfo, err := os.Stat(directory) - if err != nil { - if os.IsNotExist(err) { - return fmt.Errorf("no such directory: %s", directory) - } - return fmt.Errorf("failed to open directory: %v", err) - } - if !dirInfo.IsDir() { - return fmt.Errorf("not a directory: %s", directory) - } - - _, err = os.Stat(path.Join(directory, scanner.METADATA_FILENAME)) - if err == nil { - return fmt.Errorf("directory already initialised: %s", directory) - } - - err = scanner.WriteMetadata(directory, scanner.DefaultMetadata()) - - return err -} - -func generateOAuthToken( - ctx *context.Context, - oauth2Config *oauth2.Config, - cfg *config.Config, -) (*oauth2.Token, error) { - verifier := oauth2.GenerateVerifier() - - var token *oauth2.Token - wg := sync.WaitGroup{} - var server http.Server - server.Addr = cfg.Host - server.Handler = http.HandlerFunc( - func(w http.ResponseWriter, r *http.Request) { - if !r.URL.Query().Has("code") { - http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) - return - } - - code := r.URL.Query().Get("code") - - t, err := oauth2Config.Exchange(*ctx, code, oauth2.VerifierOption(verifier)) - if err != nil { - log.Fatalf("Could not exchange OAuth2 code: %v", err) - http.Error( w, - fmt.Sprintf("Could not exchange OAuth2 code: %v", err), - http.StatusBadRequest, - ) - return - } - - token = t - http.Error( - w, - "Authentication successful! You may now close this tab.", - http.StatusOK, - ) - if f, ok := w.(http.Flusher); ok { - f.Flush() - } - - wg.Done() - server.Close() - }, - ) - - url := oauth2Config.AuthCodeURL( - "state", - oauth2.AccessTypeOffline, - oauth2.S256ChallengeOption(verifier), - ) - fmt.Printf("\nSign in to YouTube: %s\n\n", url) - - wg.Add(1) - if err := server.ListenAndServe(); err != http.ErrServerClosed { - return nil, fmt.Errorf("http: %v", err) - } - wg.Wait() - - return token, nil -} diff --git a/oauth/oauth.go b/oauth/oauth.go new file mode 100644 index 0000000..4dee4a8 --- /dev/null +++ b/oauth/oauth.go @@ -0,0 +1,73 @@ +package oauth + +import ( + "context" + "fmt" + "log" + "net/http" + "sync" + + "arimelody.space/vodular/config" + "golang.org/x/oauth2" +) + +func GenerateToken( + ctx *context.Context, + oauth2Config *oauth2.Config, + cfg *config.Config, +) (*oauth2.Token, error) { + verifier := oauth2.GenerateVerifier() + + var token *oauth2.Token + wg := sync.WaitGroup{} + var server http.Server + server.Addr = cfg.Host + server.Handler = http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + if !r.URL.Query().Has("code") { + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) + return + } + + code := r.URL.Query().Get("code") + + t, err := oauth2Config.Exchange(*ctx, code, oauth2.VerifierOption(verifier)) + if err != nil { + log.Fatalf("Could not exchange OAuth2 code: %v", err) + http.Error( w, + fmt.Sprintf("Could not exchange OAuth2 code: %v", err), + http.StatusBadRequest, + ) + return + } + + token = t + http.Error( + w, + "Authentication successful! You may now close this tab.", + http.StatusOK, + ) + if f, ok := w.(http.Flusher); ok { + f.Flush() + } + + wg.Done() + server.Close() + }, + ) + + url := oauth2Config.AuthCodeURL( + "state", + oauth2.AccessTypeOffline, + oauth2.S256ChallengeOption(verifier), + ) + fmt.Printf("\nSign in to YouTube: %s\n\n", url) + + wg.Add(1) + if err := server.ListenAndServe(); err != http.ErrServerClosed { + return nil, fmt.Errorf("http: %v", err) + } + wg.Wait() + + return token, nil +} diff --git a/scanner/scanner.go b/scanner/scanner.go index 99aa504..157c449 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -42,6 +42,28 @@ type ( const METADATA_FILENAME = "metadata.toml" +func InitialiseDirectory(directory string) error { + dirInfo, err := os.Stat(directory) + if err != nil { + if os.IsNotExist(err) { + return fmt.Errorf("no such directory: %s", directory) + } + return fmt.Errorf("failed to open directory: %v", err) + } + if !dirInfo.IsDir() { + return fmt.Errorf("not a directory: %s", directory) + } + + _, err = os.Stat(path.Join(directory, METADATA_FILENAME)) + if err == nil { + return fmt.Errorf("directory already initialised: %s", directory) + } + + err = WriteMetadata(directory, DefaultMetadata()) + + return err +} + func ScanSegments(directory string, extension string) ([]string, error) { entries, err := os.ReadDir(directory) if err != nil {