Skip to content

Commit 2084433

Browse files
cosrnicmworzala
authored andcommitted
make requested changes
1 parent e18120f commit 2084433

File tree

10 files changed

+449
-248
lines changed

10 files changed

+449
-248
lines changed

cmd/mc/skin/add.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package skin
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
6-
"slices"
7+
"os"
8+
"os/signal"
79

810
"github.com/google/uuid"
911
"github.com/mworzala/mc/internal/pkg/cli"
12+
"github.com/mworzala/mc/internal/pkg/mojang"
1013
"github.com/spf13/cobra"
1114
)
1215

@@ -21,13 +24,12 @@ type addSkinOpts struct {
2124
}
2225

2326
var (
24-
ErrInvalidType = errors.New("invalid type")
2527
ErrInvalidVariant = errors.New("invalid variant")
2628

27-
validVariants = []string{"classic", "slim"}
29+
validationMap = map[string]bool{"classic": true, "slim": true}
2830
)
2931

30-
func newAddCmd(app *cli.App) *cobra.Command {
32+
func newAddCmd(app *cli.App, account string) *cobra.Command {
3133
var o addSkinOpts
3234

3335
cmd := &cobra.Command{
@@ -43,7 +45,8 @@ func newAddCmd(app *cli.App) *cobra.Command {
4345
},
4446
}
4547

46-
cmd.Flags().StringVar(&o.account, "account", "", "Account to use")
48+
o.account = account
49+
4750
cmd.Flags().StringVar(&o.variant, "variant", "classic", "Skin variant [classic/slim]")
4851
cmd.Flags().StringVar(&o.cape, "cape", "", "Cape name, 'none' to remove")
4952
cmd.Flags().BoolVar(&o.apply, "apply", false, "Apply the skin")
@@ -59,7 +62,7 @@ func (o *addSkinOpts) validateArgs(cmd *cobra.Command, args []string) (err error
5962
return err
6063
}
6164

62-
if !slices.Contains(validVariants, o.variant) {
65+
if !validationMap[o.variant] {
6366
return ErrInvalidVariant
6467
}
6568

@@ -84,7 +87,11 @@ func (o *addSkinOpts) execute(args []string) error {
8487
return err
8588
}
8689

87-
info, err := o.app.SkinManager().GetProfileInformation(token)
90+
client := mojang.NewProfileClient(o.app.Build.Version)
91+
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
92+
defer cancel()
93+
94+
info, err := client.ProfileInformation(ctx, token)
8895
if err != nil {
8996
return err
9097
}
@@ -112,13 +119,18 @@ func (o *addSkinOpts) execute(args []string) error {
112119

113120
if o.apply {
114121

115-
err = skin.Apply(token)
122+
err = o.app.SkinManager().ApplySkin(skin, client, ctx, token)
116123
if err != nil {
117124
return err
118125
}
126+
if !o.app.Config.NonInteractive {
127+
fmt.Printf("skin %s applied", skin.Name)
128+
}
119129
}
120130

121-
fmt.Printf("skin %s with cape %s was added to the list", skin.Name, skin.Cape)
131+
if !o.app.Config.NonInteractive {
132+
fmt.Printf("skin %s with cape %s was added to the list", skin.Name, skin.Cape)
133+
}
122134

123135
return o.app.SkinManager().Save()
124136

cmd/mc/skin/apply.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package skin
22

33
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/signal"
8+
49
"github.com/mworzala/mc/internal/pkg/cli"
10+
"github.com/mworzala/mc/internal/pkg/mojang"
511
"github.com/spf13/cobra"
612
)
713

@@ -11,7 +17,7 @@ type applySkinOpts struct {
1117
account string
1218
}
1319

14-
func newApplyCmd(app *cli.App) *cobra.Command {
20+
func newApplyCmd(app *cli.App, account string) *cobra.Command {
1521
var o applySkinOpts
1622

1723
cmd := &cobra.Command{
@@ -28,7 +34,7 @@ func newApplyCmd(app *cli.App) *cobra.Command {
2834
},
2935
}
3036

31-
cmd.Flags().StringVar(&o.account, "account", "", "Account to use")
37+
o.account = account
3238

3339
return cmd
3440
}
@@ -58,5 +64,17 @@ func (o *applySkinOpts) execute(args []string) error {
5864
return err
5965
}
6066

61-
return skin.Apply(token)
67+
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
68+
defer cancel()
69+
70+
client := mojang.NewProfileClient(o.app.Build.Version)
71+
72+
err = o.app.SkinManager().ApplySkin(skin, client, ctx, token)
73+
if err != nil {
74+
return err
75+
}
76+
if !o.app.Config.NonInteractive {
77+
fmt.Printf("skin %s applied", skin.Name)
78+
}
79+
return nil
6280
}

cmd/mc/skin/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func (o *listSkinsOpts) listSkins() error {
3232
var result appModel.SkinList
3333
for _, skin := range skinManager.Skins() {
3434
result = append(result, &appModel.Skin{
35-
Name: skin.Name,
36-
Date: skin.AddedDate,
35+
Name: skin.Name,
36+
Modified: skin.AddedDate,
3737
})
3838
}
3939

cmd/mc/skin/skin.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ import (
88
func NewSkinCmd(app *cli.App) *cobra.Command {
99
cmd := &cobra.Command{
1010
Use: "skin",
11-
Short: "Manage Minecraft Skins and Capes",
11+
Short: "Manage Minecraft skins and capes",
1212
}
1313

14+
var account string
15+
16+
cmd.Flags().StringVar(&account, "account", "", "Account to use")
17+
1418
cmd.AddCommand(newListCmd(app))
15-
cmd.AddCommand(newAddCmd(app))
16-
cmd.AddCommand(newApplyCmd(app))
19+
cmd.AddCommand(newAddCmd(app, account))
20+
cmd.AddCommand(newApplyCmd(app, account))
1721

1822
return cmd
1923
}

internal/pkg/cli/model/skin.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ import (
88
)
99

1010
type Skin struct {
11-
Name string
12-
Date time.Time
11+
Name string
12+
Modified time.Time
1313
}
1414

1515
func (i *Skin) String() string {
16-
return fmt.Sprintf("%s\t%s", i.Name, i.Date)
16+
return fmt.Sprintf("%s\t%s", i.Name, i.Modified)
1717
}
1818

1919
type SkinList []*Skin
2020

2121
func (l SkinList) String() string {
2222
table := uitable.New()
23-
table.AddRow("NAME", "DATE")
23+
table.AddRow("NAME", "MODIFIED")
2424
for _, skin := range l {
25-
table.AddRow(skin.Name, skin.Date)
25+
table.AddRow(skin.Name, skin.Modified)
2626
}
2727
return table.String()
2828
}

0 commit comments

Comments
 (0)