@@ -3,30 +3,31 @@ package zsh
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "io"
7
+ "net/http"
6
8
"os"
7
9
"path/filepath"
8
10
"strconv"
9
11
"strings"
10
12
11
- "github.com/go-git/go-git/v5"
12
13
"golang.org/x/exp/slices"
13
14
)
14
15
15
16
type Zsh struct {
16
- Zinit [] string `json:"zinit "`
17
- History struct {
17
+ Zsh4Humans bool `json:"zsh4humans "`
18
+ History struct {
18
19
Size int `json:"size"`
19
20
ShareHistory bool `json:"share_history"`
20
21
IncAppend bool `json:"inc_append"`
21
22
IgnoreAllDups bool `json:"ignore_all_dups"`
22
23
IgnoreSpace bool `json:"ignore_space"`
23
24
} `json:"history"`
24
- Paths []string `json:"paths"`
25
- Variables []KV `json:"variables"`
26
- Aliases []KV `json:"aliases"`
27
- Functions []KV `json:"functions"`
28
- ExtraPrefix string `json:"extra_prefix "`
29
- ExtraSuffix string `json:"extra_suffix "`
25
+ Paths []string `json:"paths"`
26
+ Variables []KV `json:"variables"`
27
+ Aliases []KV `json:"aliases"`
28
+ Functions []KV `json:"functions"`
29
+ Prefix string `json:"prefix "`
30
+ Suffix string `json:"suffix "`
30
31
}
31
32
32
33
type KV struct {
@@ -39,7 +40,7 @@ func (z *Zsh) Ensure(ctx context.Context) error {
39
40
return nil
40
41
}
41
42
42
- if err := z .ensureZinit (ctx ); err != nil {
43
+ if err := z .ensureZsh4Humans (ctx ); err != nil {
43
44
return fmt .Errorf ("error ensuring zinit: %w" , err )
44
45
}
45
46
home , err := os .UserHomeDir ()
@@ -53,32 +54,44 @@ func (z *Zsh) Ensure(ctx context.Context) error {
53
54
return nil
54
55
}
55
56
56
- const (
57
- zinitURL = "https://raw.githubusercontent.com/zdharma-continuum/zinit/master/zinit.zsh"
58
- )
59
-
60
- func (z * Zsh ) ensureZinit (ctx context.Context ) error {
61
- if len (z .Zinit ) == 0 {
57
+ func (z * Zsh ) ensureZsh4Humans (ctx context.Context ) error {
58
+ if ! z .Zsh4Humans {
62
59
return nil
63
60
}
64
61
65
62
home , err := os .UserHomeDir ()
66
63
if err != nil {
67
64
return fmt .Errorf ("unable to determine home dir: %w" , err )
68
65
}
69
- dstPath := filepath .Join (home , ".zinit" , "bin" , "zinit.zsh " )
66
+ zshenv := filepath .Join (home , ".zshenv " )
70
67
71
- _ , err = os .Stat (dstPath )
68
+ _ , err = os .Stat (zshenv )
72
69
if err == nil {
73
70
return nil // file already exists
74
71
} else if err != nil && ! os .IsNotExist (err ) {
75
72
return err
76
73
}
77
74
78
- fmt .Println ("installing Zinit" )
79
- _ , err = git .PlainCloneContext (ctx , filepath .Join (home , ".zinit" , "bin" ), false , & git.CloneOptions {URL : "https://github.com/zdharma-continuum/zinit.git" })
75
+ fmt .Println ("writing zsh4humans .zshenv file" )
76
+ url := "https://raw.githubusercontent.com/romkatv/zsh4humans/v5/.zshenv"
77
+ req , err := http .NewRequestWithContext (ctx , "GET" , url , nil )
78
+ if err != nil {
79
+ return fmt .Errorf ("building request to fetch zsh4humans .zshenv: %w" , err )
80
+ }
81
+ resp , err := http .DefaultClient .Do (req )
82
+ if err != nil {
83
+ return fmt .Errorf ("fetching zsh4humans .zshenv: %w" , err )
84
+ }
85
+ defer resp .Body .Close ()
86
+ body , err := io .ReadAll (resp .Body )
80
87
if err != nil {
81
- return fmt .Errorf ("error cloning zinit: %w" , err )
88
+ return fmt .Errorf ("reading zsh4humans .zshenv body: %w" , err )
89
+ }
90
+ if resp .StatusCode < http .StatusOK || resp .StatusCode >= http .StatusBadRequest {
91
+ return fmt .Errorf ("bad response fetching %s (%d): %s" , url , resp .StatusCode , string (body ))
92
+ }
93
+ if err := os .WriteFile (zshenv , body , 0o644 ); err != nil {
94
+ return fmt .Errorf ("writing .zshenv: %w" , err )
82
95
}
83
96
return nil
84
97
}
@@ -87,13 +100,7 @@ func (z *Zsh) String() string {
87
100
var sb strings.Builder
88
101
89
102
// extra prefix
90
- sb .WriteString (z .ExtraPrefix )
91
- sb .WriteString ("\n " )
92
-
93
- // zinit
94
- for _ , line := range z .Zinit {
95
- sb .WriteString (fmt .Sprintf ("zinit %s\n " , line ))
96
- }
103
+ sb .WriteString (z .Prefix )
97
104
sb .WriteString ("\n " )
98
105
99
106
// history
@@ -147,7 +154,7 @@ func (z *Zsh) String() string {
147
154
sb .WriteString ("\n " )
148
155
149
156
// extra suffix
150
- sb .WriteString (z .ExtraSuffix )
157
+ sb .WriteString (z .Suffix )
151
158
sb .WriteString ("\n " )
152
159
return sb .String ()
153
160
}
0 commit comments