Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Music polishing 1 (only to get the rabbit running) #97

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions agent/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ include ../utils/Makefile.common
install:
install -s -b -c ${PROG} /usr/local/libexec/

clean:
rm -f $(PROG)
rm -f *~ */*~
4 changes: 4 additions & 0 deletions cli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ include ../utils/Makefile.common
install:
install -s -b -c ${PROG} /usr/local/bin/

clean:
rm -f $(PROG)
rm -f *~ */*~
johanix marked this conversation as resolved.
Show resolved Hide resolved


25 changes: 14 additions & 11 deletions cli/cmd/shared_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,40 @@ import (
)

func init() {
// From ../libcli/start_cmds.go:
// From ../tdns/cli/db_cmds.go:
rootCmd.AddCommand(cli.DbCmd)

// From ../tdns/cli/start_cmds.go:
rootCmd.AddCommand(cli.PingCmd)
rootCmd.AddCommand(cli.DaemonCmd)

// From ../libcli/zone_cmds.go:
// From ../tdns/cli/zone_cmds.go:
rootCmd.AddCommand(cli.ZoneCmd)

// From ../libcli/ddns_cmds.go:
// From ../tdns/cli/ddns_cmds.go:
rootCmd.AddCommand(cli.DdnsCmd, cli.DelCmd)

// From ../libcli/debug_cmds.go:
// From ../tdns/cli/debug_cmds.go:
rootCmd.AddCommand(cli.DebugCmd)

// From ../libcli/keystore_cmds.go:
// From ../tdns/cli/keystore_cmds.go:
rootCmd.AddCommand(cli.KeystoreCmd)

// From ../libcli/truststore_cmds.go:
// From ../tdns/cli/truststore_cmds.go:
rootCmd.AddCommand(cli.TruststoreCmd)

// From ../libcli/dsync_cmds.go:
// From ../tdns/cli/dsync_cmds.go:
rootCmd.AddCommand(cli.DsyncDiscoveryCmd)

// From ../libcli/config_cmds.go:
// From ../tdns/cli/config_cmds.go:
rootCmd.AddCommand(cli.ConfigCmd)

// From ../libcli/rfc3597.go:
// From ../tdns/cli/rfc3597.go:
rootCmd.AddCommand(cli.ToRFC3597Cmd)

// From ../libcli/notify_cmds.go:
// From ../tdns/cli/notify_cmds.go:
rootCmd.AddCommand(cli.NotifyCmd)

// From ../libcli/commands.go:
// From ../tdns/cli/commands.go:
rootCmd.AddCommand(cli.StopCmd)
}
3 changes: 3 additions & 0 deletions dog/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ include ../utils/Makefile.common
install:
install -s -b -c ${PROG} /usr/local/bin/

clean:
rm -f $(PROG)
rm -f *~ */*~
52 changes: 52 additions & 0 deletions music/cmd/db_cmds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2024 Johan Stenstam, [email protected]
*/

package mcmd

import (
"fmt"
"log"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var musicDbFile string
var DbCmd = &cobra.Command{
Use: "db",
Short: "MUSIC DB commands",
}

var dbInitCmd = &cobra.Command{
Use: "init",
Short: "Initialize MUSIC DB",
Run: func(cmd *cobra.Command, args []string) {
if musicDbFile == "" {
musicDbFile = viper.GetString("db.file")
}
if musicDbFile == "" {
log.Fatalf("Error: MUSIC DB file not specified in config nor on command line")
}

if _, err := os.Stat(musicDbFile); err == nil {
fmt.Printf("Warning: MUSIC DB file '%s' already exists.\n", musicDbFile)
} else if os.IsNotExist(err) {
file, err := os.Create(musicDbFile)
if err != nil {
log.Fatalf("Error creating MUSIC DB file '%s': %v", musicDbFile, err)
}
defer file.Close()
fmt.Printf("MUSIC DB file '%s' created successfully.\n", musicDbFile)
} else {
log.Fatalf("Error checking MUSIC DB file '%s': %v", musicDbFile, err)
}
Comment on lines +33 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add security measures and improve error handling

Several improvements could be made to the file creation logic:

  1. Set explicit file permissions
  2. Validate the file path
  3. Use atomic operations to prevent race conditions

Here's a suggested implementation:

-		if _, err := os.Stat(musicDbFile); err == nil {
-			fmt.Printf("Warning: MUSIC DB file '%s' already exists.\n", musicDbFile)
-		} else if os.IsNotExist(err) {
-			file, err := os.Create(musicDbFile)
+		// Validate file path
+		dir := filepath.Dir(musicDbFile)
+		if _, err := os.Stat(dir); err != nil {
+			log.Fatalf("Invalid directory for database file: %v", err)
+		}
+
+		// Attempt to create file with specific permissions
+		file, err := os.OpenFile(musicDbFile, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
+		if err != nil {
+			if os.IsExist(err) {
+				fmt.Printf("Warning: MUSIC DB file '%s' already exists.\n", musicDbFile)
+				return
+			}
 			if err != nil {
 				log.Fatalf("Error creating MUSIC DB file '%s': %v", musicDbFile, err)
 			}
-			defer file.Close()
-			fmt.Printf("MUSIC DB file '%s' created successfully.\n", musicDbFile)
-		} else {
-			log.Fatalf("Error checking MUSIC DB file '%s': %v", musicDbFile, err)
 		}
+		defer file.Close()
+		fmt.Printf("MUSIC DB file '%s' created successfully.\n", musicDbFile)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if _, err := os.Stat(musicDbFile); err == nil {
fmt.Printf("Warning: MUSIC DB file '%s' already exists.\n", musicDbFile)
} else if os.IsNotExist(err) {
file, err := os.Create(musicDbFile)
if err != nil {
log.Fatalf("Error creating MUSIC DB file '%s': %v", musicDbFile, err)
}
defer file.Close()
fmt.Printf("MUSIC DB file '%s' created successfully.\n", musicDbFile)
} else {
log.Fatalf("Error checking MUSIC DB file '%s': %v", musicDbFile, err)
}
// Validate file path
dir := filepath.Dir(musicDbFile)
if _, err := os.Stat(dir); err != nil {
log.Fatalf("Invalid directory for database file: %v", err)
}
// Attempt to create file with specific permissions
file, err := os.OpenFile(musicDbFile, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
if os.IsExist(err) {
fmt.Printf("Warning: MUSIC DB file '%s' already exists.\n", musicDbFile)
return
}
if err != nil {
log.Fatalf("Error creating MUSIC DB file '%s': %v", musicDbFile, err)
}
}
defer file.Close()
fmt.Printf("MUSIC DB file '%s' created successfully.\n", musicDbFile)

},
}

func init() {
DbCmd.AddCommand(dbInitCmd)

dbInitCmd.Flags().StringVarP(&musicDbFile, "file", "f", "", "MUSIC DB file")
}
23 changes: 22 additions & 1 deletion music/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package mcmd

import (
"bytes"
"encoding/json"
"fmt"
"log"

Expand Down Expand Up @@ -66,6 +68,25 @@ func InitApi() {
tdns.Globals.Api = tdns.NewClient("sidecar-cli", baseurl, apikey, authmethod, "insecure", tdns.Globals.Verbose, tdns.Globals.Debug)

if tdns.Globals.Debug {
fmt.Printf("initApi: api connection to %s initialized (%s)\n:\napi: %+v\n", baseurl, apikey, tdns.Globals.Api)
tmpapi := tdns.ApiClient{
Name: tdns.Globals.Api.Name,
BaseUrl: tdns.Globals.Api.BaseUrl,
AuthMethod: tdns.Globals.Api.AuthMethod,
Client: nil,
Verbose: tdns.Globals.Verbose,
Debug: tdns.Globals.Debug,
UseTLS: tdns.Globals.Api.UseTLS,
}
Comment on lines +71 to +79
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix potential marshaling issue with Client field

The Client field is set to nil, but the static analysis suggests there might be marshaling issues with the http.Client type. Consider using a custom MarshalJSON method or excluding the field using json tags.

 tmpapi := tdns.ApiClient{
     Name:       tdns.Globals.Api.Name,
     BaseUrl:    tdns.Globals.Api.BaseUrl,
     AuthMethod: tdns.Globals.Api.AuthMethod,
-    Client:     nil,
     Verbose:    tdns.Globals.Verbose,
     Debug:      tdns.Globals.Debug,
     UseTLS:     tdns.Globals.Api.UseTLS,
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
tmpapi := tdns.ApiClient{
Name: tdns.Globals.Api.Name,
BaseUrl: tdns.Globals.Api.BaseUrl,
AuthMethod: tdns.Globals.Api.AuthMethod,
Client: nil,
Verbose: tdns.Globals.Verbose,
Debug: tdns.Globals.Debug,
UseTLS: tdns.Globals.Api.UseTLS,
}
tmpapi := tdns.ApiClient{
Name: tdns.Globals.Api.Name,
BaseUrl: tdns.Globals.Api.BaseUrl,
AuthMethod: tdns.Globals.Api.AuthMethod,
Verbose: tdns.Globals.Verbose,
Debug: tdns.Globals.Debug,
UseTLS: tdns.Globals.Api.UseTLS,
}

bytebuf := new(bytes.Buffer)
err := json.NewEncoder(bytebuf).Encode(tmpapi)
if err != nil {
log.Fatalf("Error from json.NewEncoder: %v", err)
}
var prettyJSON bytes.Buffer
error := json.Indent(&prettyJSON, bytebuf.Bytes(), "", " ")
if error != nil {
log.Println("JSON parse error: ", error)
}
fmt.Printf("initApi: api connection to %s initialized (%s)\n:\napi: %s\n", baseurl, apikey, prettyJSON.String())
}
}
35 changes: 0 additions & 35 deletions music/cmd/tdns_cmds.go.foo

This file was deleted.

Loading