Skip to content

Commit

Permalink
chore: add gofmt lefthook config
Browse files Browse the repository at this point in the history
  • Loading branch information
ledif committed Feb 25, 2025
1 parent 0525d71 commit d5401bf
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 277 deletions.
10 changes: 10 additions & 0 deletions lefthook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pre-commit:
commands:
gofmt:
run: |
set -e
for file in $(git diff --cached --name-only --diff-filter=ACM | grep '\.go$'); do
gofmt -w "$file"
git add "$file"
done
glob: "*.go"
89 changes: 44 additions & 45 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,69 +1,68 @@
package main

import (
"fmt"
"github.com/spf13/cobra"
"os"
"fmt"
"github.com/spf13/cobra"
"os"
)


var applyCmd = &cobra.Command{
Use: "apply [path]",
Short: "Apply changes from a profile.json",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := applyProfile(args[0])
if err != nil {
fmt.Printf("Error: %s\n", err)
}
},
Use: "apply [path]",
Short: "Apply changes from a profile.json",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := applyProfile(args[0])
if err != nil {
fmt.Printf("Error: %s\n", err)
}
},
}

var revertCmd = &cobra.Command{
Use: "revert [path]",
Short: "Revert changes from a profile.json",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := revertProfile(args[0])
if err != nil {
fmt.Printf("Error: %s\n", err)
}
},
Use: "revert [path]",
Short: "Revert changes from a profile.json",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := revertProfile(args[0])
if err != nil {
fmt.Printf("Error: %s\n", err)
}
},
}

var recordCmd = &cobra.Command{
Use: "record",
Short: "Record changes to xsettings and dump them as a profile on SIGINT",
Run: func(cmd *cobra.Command, args []string) {
recordProfile()
},
Use: "record",
Short: "Record changes to xsettings and dump them as a profile on SIGINT",
Run: func(cmd *cobra.Command, args []string) {
recordProfile()
},
}

var syncCmd = &cobra.Command{
Use: "sync",
Short: "Sync user profile with distribution's recommended profile",
Run: func(cmd *cobra.Command, args []string) {
distProfile, _ := cmd.Flags().GetString("profile")
if err := syncProfile(distProfile); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
},
Use: "sync",
Short: "Sync user profile with distribution's recommended profile",
Run: func(cmd *cobra.Command, args []string) {
distProfile, _ := cmd.Flags().GetString("profile")
if err := syncProfile(distProfile); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
},
}

func init() {
syncCmd.Flags().StringP("profile", "p", "/usr/share/xfconf-profile/default.json", "Path to the distribution's recommended profile")
}

func main() {
var rootCmd = &cobra.Command{
Use: "xfconf-profile",
Short: "Tool for applying, reverting and managing Xfce profiles",
}
rootCmd.AddCommand(applyCmd, revertCmd, recordCmd, syncCmd)
var rootCmd = &cobra.Command{
Use: "xfconf-profile",
Short: "Tool for applying, reverting and managing Xfce profiles",
}
rootCmd.AddCommand(applyCmd, revertCmd, recordCmd, syncCmd)

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
149 changes: 74 additions & 75 deletions profile.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,83 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/fatih/color"
"encoding/json"
"errors"
"fmt"
"github.com/fatih/color"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)

func applyProfile(profilePath string) error {
data, err := ioutil.ReadFile(profilePath)
if err != nil {
return fmt.Errorf("failed to read file: %v", err)
}

var profile map[string]map[string]interface{}
err = json.Unmarshal(data, &profile)
if err != nil {
return fmt.Errorf("failed to parse JSON: %v", err)
}

blue := color.New(color.FgHiBlue).SprintFunc()

for channel, properties := range profile {
// Keys starting with X- are not channels
if strings.HasPrefix(channel, "X-") {
continue
}
for property, value := range properties {
fmt.Printf("%s Setting %s::%s ➔ %s\n", blue("•"), channel, property, value)
cmd := exec.Command("xfconf-query", "-c", channel, "--property", property, "--type", "string", "--create", "--set", fmt.Sprintf("%v", value))

output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to run command: %v\nOutput: %s", err, string(output))
}
}
}

return nil
data, err := ioutil.ReadFile(profilePath)
if err != nil {
return fmt.Errorf("failed to read file: %v", err)
}

var profile map[string]map[string]interface{}
err = json.Unmarshal(data, &profile)
if err != nil {
return fmt.Errorf("failed to parse JSON: %v", err)
}

blue := color.New(color.FgHiBlue).SprintFunc()

for channel, properties := range profile {
// Keys starting with X- are not channels
if strings.HasPrefix(channel, "X-") {
continue
}
for property, value := range properties {
fmt.Printf("%s Setting %s::%s ➔ %s\n", blue("•"), channel, property, value)
cmd := exec.Command("xfconf-query", "-c", channel, "--property", property, "--type", "string", "--create", "--set", fmt.Sprintf("%v", value))

output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to run command: %v\nOutput: %s", err, string(output))
}
}
}

return nil
}

func revertProfile(profilePath string) error {
data, err := ioutil.ReadFile(profilePath)
if err != nil {
return fmt.Errorf("failed to read file: %v", err)
}

var profile map[string]map[string]interface{}
err = json.Unmarshal(data, &profile)
if err != nil {
return fmt.Errorf("failed to parse JSON: %v", err)
}

yellow := color.New(color.FgHiYellow).SprintFunc()

for channel, properties := range profile {
// Keys starting with X- are not channels
if strings.HasPrefix(channel, "X-") {
continue
}

for property := range properties {
fmt.Printf("%s Resetting %s::%s\n", yellow("•"), channel, property)
cmd := exec.Command("xfconf-query", "-c", channel, "--reset", "--property", property)

output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to run command: %v\nOutput: %s", err, string(output))
}
}
}

return nil
}
data, err := ioutil.ReadFile(profilePath)
if err != nil {
return fmt.Errorf("failed to read file: %v", err)
}

var profile map[string]map[string]interface{}
err = json.Unmarshal(data, &profile)
if err != nil {
return fmt.Errorf("failed to parse JSON: %v", err)
}

yellow := color.New(color.FgHiYellow).SprintFunc()

for channel, properties := range profile {
// Keys starting with X- are not channels
if strings.HasPrefix(channel, "X-") {
continue
}

for property := range properties {
fmt.Printf("%s Resetting %s::%s\n", yellow("•"), channel, property)
cmd := exec.Command("xfconf-query", "-c", channel, "--reset", "--property", property)

output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to run command: %v\nOutput: %s", err, string(output))
}
}
}

return nil
}

// Create $XDG_STATE_HOME/xfconf-profile/sync if needed
func ensureStateDir() (string, error) {
Expand Down Expand Up @@ -136,10 +135,10 @@ func syncProfile(distConfig string) error {
return err
}

_, err = os.Stat(distConfig)
if err != nil {
return err
}
_, err = os.Stat(distConfig)
if err != nil {
return err
}

currentDir := filepath.Join(stateDirPath, "current")
previousDir := filepath.Join(stateDirPath, "previous")
Expand Down
Loading

0 comments on commit d5401bf

Please sign in to comment.