diff --git a/db/db.go b/db/db.go index a6e4481..1ef505b 100644 --- a/db/db.go +++ b/db/db.go @@ -66,9 +66,13 @@ func (c *Client) init() { log.Infof("set default log level") c.SetSetting(SettingLogLevel, "info") } - if tr := c.GetTransmission(); tr == nil { + if tr := c.GetAllDonloadClients(); len(tr) == 0 { log.Warnf("no download client, set default download client") - c.SaveTransmission("transmission", "http://transmission:9091", "", "") + c.SaveDownloader(&ent.DownloadClients{ + Name: "transmission", + Implementation: downloadclients.ImplementationTransmission, + URL: "http://transmission:9091", + }) } } @@ -322,30 +326,22 @@ func (c *Client) GetAllTorznabInfo() []*TorznabInfo { return l } -func (c *Client) SaveTransmission(name, url, user, password string) error { - count := c.ent.DownloadClients.Query().Where(downloadclients.Name(name)).CountX(context.TODO()) +func (c *Client) SaveDownloader(downloader *ent.DownloadClients) error { + count := c.ent.DownloadClients.Query().Where(downloadclients.Name(downloader.Name)).CountX(context.TODO()) if count != 0 { - err := c.ent.DownloadClients.Update().Where(downloadclients.Name(name)). - SetURL(url).SetUser(user).SetPassword(password).Exec(context.TODO()) + err := c.ent.DownloadClients.Update().Where(downloadclients.Name(downloader.Name)).SetImplementation(downloader.Implementation). + SetURL(downloader.URL).SetUser(downloader.User).SetPassword(downloader.Password).Exec(context.TODO()) return err } - _, err := c.ent.DownloadClients.Create().SetEnable(true).SetImplementation("transmission"). - SetName(name).SetURL(url).SetUser(user).SetPassword(password).Save(context.TODO()) + _, err := c.ent.DownloadClients.Create().SetEnable(true).SetImplementation(downloader.Implementation). + SetName(downloader.Name).SetURL(downloader.URL).SetUser(downloader.User).SetPassword(downloader.Password).Save(context.TODO()) return err } -func (c *Client) GetTransmission() *ent.DownloadClients { - dc, err := c.ent.DownloadClients.Query().Where(downloadclients.Implementation("transmission")).First(context.TODO()) - if err != nil { - log.Errorf("no transmission client found: %v", err) - return nil - } - return dc -} func (c *Client) GetAllDonloadClients() []*ent.DownloadClients { - cc, err := c.ent.DownloadClients.Query().All(context.TODO()) + cc, err := c.ent.DownloadClients.Query().Order(ent.Asc(downloadclients.FieldOrdering)).All(context.TODO()) if err != nil { log.Errorf("no download client") return nil diff --git a/ent/downloadclients.go b/ent/downloadclients.go index d60d6d5..f58b3fb 100644 --- a/ent/downloadclients.go +++ b/ent/downloadclients.go @@ -21,7 +21,7 @@ type DownloadClients struct { // Name holds the value of the "name" field. Name string `json:"name,omitempty"` // Implementation holds the value of the "implementation" field. - Implementation string `json:"implementation,omitempty"` + Implementation downloadclients.Implementation `json:"implementation,omitempty"` // URL holds the value of the "url" field. URL string `json:"url,omitempty"` // User holds the value of the "user" field. @@ -30,8 +30,8 @@ type DownloadClients struct { Password string `json:"password,omitempty"` // Settings holds the value of the "settings" field. Settings string `json:"settings,omitempty"` - // Priority holds the value of the "priority" field. - Priority string `json:"priority,omitempty"` + // Ordering holds the value of the "ordering" field. + Ordering int `json:"ordering,omitempty"` // RemoveCompletedDownloads holds the value of the "remove_completed_downloads" field. RemoveCompletedDownloads bool `json:"remove_completed_downloads,omitempty"` // RemoveFailedDownloads holds the value of the "remove_failed_downloads" field. @@ -48,9 +48,9 @@ func (*DownloadClients) scanValues(columns []string) ([]any, error) { switch columns[i] { case downloadclients.FieldEnable, downloadclients.FieldRemoveCompletedDownloads, downloadclients.FieldRemoveFailedDownloads: values[i] = new(sql.NullBool) - case downloadclients.FieldID: + case downloadclients.FieldID, downloadclients.FieldOrdering: values[i] = new(sql.NullInt64) - case downloadclients.FieldName, downloadclients.FieldImplementation, downloadclients.FieldURL, downloadclients.FieldUser, downloadclients.FieldPassword, downloadclients.FieldSettings, downloadclients.FieldPriority, downloadclients.FieldTags: + case downloadclients.FieldName, downloadclients.FieldImplementation, downloadclients.FieldURL, downloadclients.FieldUser, downloadclients.FieldPassword, downloadclients.FieldSettings, downloadclients.FieldTags: values[i] = new(sql.NullString) default: values[i] = new(sql.UnknownType) @@ -89,7 +89,7 @@ func (dc *DownloadClients) assignValues(columns []string, values []any) error { if value, ok := values[i].(*sql.NullString); !ok { return fmt.Errorf("unexpected type %T for field implementation", values[i]) } else if value.Valid { - dc.Implementation = value.String + dc.Implementation = downloadclients.Implementation(value.String) } case downloadclients.FieldURL: if value, ok := values[i].(*sql.NullString); !ok { @@ -115,11 +115,11 @@ func (dc *DownloadClients) assignValues(columns []string, values []any) error { } else if value.Valid { dc.Settings = value.String } - case downloadclients.FieldPriority: - if value, ok := values[i].(*sql.NullString); !ok { - return fmt.Errorf("unexpected type %T for field priority", values[i]) + case downloadclients.FieldOrdering: + if value, ok := values[i].(*sql.NullInt64); !ok { + return fmt.Errorf("unexpected type %T for field ordering", values[i]) } else if value.Valid { - dc.Priority = value.String + dc.Ordering = int(value.Int64) } case downloadclients.FieldRemoveCompletedDownloads: if value, ok := values[i].(*sql.NullBool); !ok { @@ -182,7 +182,7 @@ func (dc *DownloadClients) String() string { builder.WriteString(dc.Name) builder.WriteString(", ") builder.WriteString("implementation=") - builder.WriteString(dc.Implementation) + builder.WriteString(fmt.Sprintf("%v", dc.Implementation)) builder.WriteString(", ") builder.WriteString("url=") builder.WriteString(dc.URL) @@ -196,8 +196,8 @@ func (dc *DownloadClients) String() string { builder.WriteString("settings=") builder.WriteString(dc.Settings) builder.WriteString(", ") - builder.WriteString("priority=") - builder.WriteString(dc.Priority) + builder.WriteString("ordering=") + builder.WriteString(fmt.Sprintf("%v", dc.Ordering)) builder.WriteString(", ") builder.WriteString("remove_completed_downloads=") builder.WriteString(fmt.Sprintf("%v", dc.RemoveCompletedDownloads)) diff --git a/ent/downloadclients/downloadclients.go b/ent/downloadclients/downloadclients.go index f83c5ba..3b23e76 100644 --- a/ent/downloadclients/downloadclients.go +++ b/ent/downloadclients/downloadclients.go @@ -3,6 +3,8 @@ package downloadclients import ( + "fmt" + "entgo.io/ent/dialect/sql" ) @@ -25,8 +27,8 @@ const ( FieldPassword = "password" // FieldSettings holds the string denoting the settings field in the database. FieldSettings = "settings" - // FieldPriority holds the string denoting the priority field in the database. - FieldPriority = "priority" + // FieldOrdering holds the string denoting the ordering field in the database. + FieldOrdering = "ordering" // FieldRemoveCompletedDownloads holds the string denoting the remove_completed_downloads field in the database. FieldRemoveCompletedDownloads = "remove_completed_downloads" // FieldRemoveFailedDownloads holds the string denoting the remove_failed_downloads field in the database. @@ -47,7 +49,7 @@ var Columns = []string{ FieldUser, FieldPassword, FieldSettings, - FieldPriority, + FieldOrdering, FieldRemoveCompletedDownloads, FieldRemoveFailedDownloads, FieldTags, @@ -70,8 +72,10 @@ var ( DefaultPassword string // DefaultSettings holds the default value on creation for the "settings" field. DefaultSettings string - // DefaultPriority holds the default value on creation for the "priority" field. - DefaultPriority string + // DefaultOrdering holds the default value on creation for the "ordering" field. + DefaultOrdering int + // OrderingValidator is a validator for the "ordering" field. It is called by the builders before save. + OrderingValidator func(int) error // DefaultRemoveCompletedDownloads holds the default value on creation for the "remove_completed_downloads" field. DefaultRemoveCompletedDownloads bool // DefaultRemoveFailedDownloads holds the default value on creation for the "remove_failed_downloads" field. @@ -80,6 +84,29 @@ var ( DefaultTags string ) +// Implementation defines the type for the "implementation" enum field. +type Implementation string + +// Implementation values. +const ( + ImplementationTransmission Implementation = "transmission" + ImplementationQbittorrent Implementation = "qbittorrent" +) + +func (i Implementation) String() string { + return string(i) +} + +// ImplementationValidator is a validator for the "implementation" field enum values. It is called by the builders before save. +func ImplementationValidator(i Implementation) error { + switch i { + case ImplementationTransmission, ImplementationQbittorrent: + return nil + default: + return fmt.Errorf("downloadclients: invalid enum value for implementation field: %q", i) + } +} + // OrderOption defines the ordering options for the DownloadClients queries. type OrderOption func(*sql.Selector) @@ -123,9 +150,9 @@ func BySettings(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldSettings, opts...).ToFunc() } -// ByPriority orders the results by the priority field. -func ByPriority(opts ...sql.OrderTermOption) OrderOption { - return sql.OrderByField(FieldPriority, opts...).ToFunc() +// ByOrdering orders the results by the ordering field. +func ByOrdering(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldOrdering, opts...).ToFunc() } // ByRemoveCompletedDownloads orders the results by the remove_completed_downloads field. diff --git a/ent/downloadclients/where.go b/ent/downloadclients/where.go index 38fa7f0..bd3be90 100644 --- a/ent/downloadclients/where.go +++ b/ent/downloadclients/where.go @@ -63,11 +63,6 @@ func Name(v string) predicate.DownloadClients { return predicate.DownloadClients(sql.FieldEQ(FieldName, v)) } -// Implementation applies equality check predicate on the "implementation" field. It's identical to ImplementationEQ. -func Implementation(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldEQ(FieldImplementation, v)) -} - // URL applies equality check predicate on the "url" field. It's identical to URLEQ. func URL(v string) predicate.DownloadClients { return predicate.DownloadClients(sql.FieldEQ(FieldURL, v)) @@ -88,9 +83,9 @@ func Settings(v string) predicate.DownloadClients { return predicate.DownloadClients(sql.FieldEQ(FieldSettings, v)) } -// Priority applies equality check predicate on the "priority" field. It's identical to PriorityEQ. -func Priority(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldEQ(FieldPriority, v)) +// Ordering applies equality check predicate on the "ordering" field. It's identical to OrderingEQ. +func Ordering(v int) predicate.DownloadClients { + return predicate.DownloadClients(sql.FieldEQ(FieldOrdering, v)) } // RemoveCompletedDownloads applies equality check predicate on the "remove_completed_downloads" field. It's identical to RemoveCompletedDownloadsEQ. @@ -184,70 +179,25 @@ func NameContainsFold(v string) predicate.DownloadClients { } // ImplementationEQ applies the EQ predicate on the "implementation" field. -func ImplementationEQ(v string) predicate.DownloadClients { +func ImplementationEQ(v Implementation) predicate.DownloadClients { return predicate.DownloadClients(sql.FieldEQ(FieldImplementation, v)) } // ImplementationNEQ applies the NEQ predicate on the "implementation" field. -func ImplementationNEQ(v string) predicate.DownloadClients { +func ImplementationNEQ(v Implementation) predicate.DownloadClients { return predicate.DownloadClients(sql.FieldNEQ(FieldImplementation, v)) } // ImplementationIn applies the In predicate on the "implementation" field. -func ImplementationIn(vs ...string) predicate.DownloadClients { +func ImplementationIn(vs ...Implementation) predicate.DownloadClients { return predicate.DownloadClients(sql.FieldIn(FieldImplementation, vs...)) } // ImplementationNotIn applies the NotIn predicate on the "implementation" field. -func ImplementationNotIn(vs ...string) predicate.DownloadClients { +func ImplementationNotIn(vs ...Implementation) predicate.DownloadClients { return predicate.DownloadClients(sql.FieldNotIn(FieldImplementation, vs...)) } -// ImplementationGT applies the GT predicate on the "implementation" field. -func ImplementationGT(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldGT(FieldImplementation, v)) -} - -// ImplementationGTE applies the GTE predicate on the "implementation" field. -func ImplementationGTE(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldGTE(FieldImplementation, v)) -} - -// ImplementationLT applies the LT predicate on the "implementation" field. -func ImplementationLT(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldLT(FieldImplementation, v)) -} - -// ImplementationLTE applies the LTE predicate on the "implementation" field. -func ImplementationLTE(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldLTE(FieldImplementation, v)) -} - -// ImplementationContains applies the Contains predicate on the "implementation" field. -func ImplementationContains(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldContains(FieldImplementation, v)) -} - -// ImplementationHasPrefix applies the HasPrefix predicate on the "implementation" field. -func ImplementationHasPrefix(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldHasPrefix(FieldImplementation, v)) -} - -// ImplementationHasSuffix applies the HasSuffix predicate on the "implementation" field. -func ImplementationHasSuffix(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldHasSuffix(FieldImplementation, v)) -} - -// ImplementationEqualFold applies the EqualFold predicate on the "implementation" field. -func ImplementationEqualFold(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldEqualFold(FieldImplementation, v)) -} - -// ImplementationContainsFold applies the ContainsFold predicate on the "implementation" field. -func ImplementationContainsFold(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldContainsFold(FieldImplementation, v)) -} - // URLEQ applies the EQ predicate on the "url" field. func URLEQ(v string) predicate.DownloadClients { return predicate.DownloadClients(sql.FieldEQ(FieldURL, v)) @@ -508,69 +458,44 @@ func SettingsContainsFold(v string) predicate.DownloadClients { return predicate.DownloadClients(sql.FieldContainsFold(FieldSettings, v)) } -// PriorityEQ applies the EQ predicate on the "priority" field. -func PriorityEQ(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldEQ(FieldPriority, v)) +// OrderingEQ applies the EQ predicate on the "ordering" field. +func OrderingEQ(v int) predicate.DownloadClients { + return predicate.DownloadClients(sql.FieldEQ(FieldOrdering, v)) } -// PriorityNEQ applies the NEQ predicate on the "priority" field. -func PriorityNEQ(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldNEQ(FieldPriority, v)) +// OrderingNEQ applies the NEQ predicate on the "ordering" field. +func OrderingNEQ(v int) predicate.DownloadClients { + return predicate.DownloadClients(sql.FieldNEQ(FieldOrdering, v)) } -// PriorityIn applies the In predicate on the "priority" field. -func PriorityIn(vs ...string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldIn(FieldPriority, vs...)) +// OrderingIn applies the In predicate on the "ordering" field. +func OrderingIn(vs ...int) predicate.DownloadClients { + return predicate.DownloadClients(sql.FieldIn(FieldOrdering, vs...)) } -// PriorityNotIn applies the NotIn predicate on the "priority" field. -func PriorityNotIn(vs ...string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldNotIn(FieldPriority, vs...)) +// OrderingNotIn applies the NotIn predicate on the "ordering" field. +func OrderingNotIn(vs ...int) predicate.DownloadClients { + return predicate.DownloadClients(sql.FieldNotIn(FieldOrdering, vs...)) } -// PriorityGT applies the GT predicate on the "priority" field. -func PriorityGT(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldGT(FieldPriority, v)) +// OrderingGT applies the GT predicate on the "ordering" field. +func OrderingGT(v int) predicate.DownloadClients { + return predicate.DownloadClients(sql.FieldGT(FieldOrdering, v)) } -// PriorityGTE applies the GTE predicate on the "priority" field. -func PriorityGTE(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldGTE(FieldPriority, v)) +// OrderingGTE applies the GTE predicate on the "ordering" field. +func OrderingGTE(v int) predicate.DownloadClients { + return predicate.DownloadClients(sql.FieldGTE(FieldOrdering, v)) } -// PriorityLT applies the LT predicate on the "priority" field. -func PriorityLT(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldLT(FieldPriority, v)) +// OrderingLT applies the LT predicate on the "ordering" field. +func OrderingLT(v int) predicate.DownloadClients { + return predicate.DownloadClients(sql.FieldLT(FieldOrdering, v)) } -// PriorityLTE applies the LTE predicate on the "priority" field. -func PriorityLTE(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldLTE(FieldPriority, v)) -} - -// PriorityContains applies the Contains predicate on the "priority" field. -func PriorityContains(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldContains(FieldPriority, v)) -} - -// PriorityHasPrefix applies the HasPrefix predicate on the "priority" field. -func PriorityHasPrefix(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldHasPrefix(FieldPriority, v)) -} - -// PriorityHasSuffix applies the HasSuffix predicate on the "priority" field. -func PriorityHasSuffix(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldHasSuffix(FieldPriority, v)) -} - -// PriorityEqualFold applies the EqualFold predicate on the "priority" field. -func PriorityEqualFold(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldEqualFold(FieldPriority, v)) -} - -// PriorityContainsFold applies the ContainsFold predicate on the "priority" field. -func PriorityContainsFold(v string) predicate.DownloadClients { - return predicate.DownloadClients(sql.FieldContainsFold(FieldPriority, v)) +// OrderingLTE applies the LTE predicate on the "ordering" field. +func OrderingLTE(v int) predicate.DownloadClients { + return predicate.DownloadClients(sql.FieldLTE(FieldOrdering, v)) } // RemoveCompletedDownloadsEQ applies the EQ predicate on the "remove_completed_downloads" field. diff --git a/ent/downloadclients_create.go b/ent/downloadclients_create.go index 41dd099..b329ad1 100644 --- a/ent/downloadclients_create.go +++ b/ent/downloadclients_create.go @@ -32,8 +32,8 @@ func (dcc *DownloadClientsCreate) SetName(s string) *DownloadClientsCreate { } // SetImplementation sets the "implementation" field. -func (dcc *DownloadClientsCreate) SetImplementation(s string) *DownloadClientsCreate { - dcc.mutation.SetImplementation(s) +func (dcc *DownloadClientsCreate) SetImplementation(d downloadclients.Implementation) *DownloadClientsCreate { + dcc.mutation.SetImplementation(d) return dcc } @@ -85,16 +85,16 @@ func (dcc *DownloadClientsCreate) SetNillableSettings(s *string) *DownloadClient return dcc } -// SetPriority sets the "priority" field. -func (dcc *DownloadClientsCreate) SetPriority(s string) *DownloadClientsCreate { - dcc.mutation.SetPriority(s) +// SetOrdering sets the "ordering" field. +func (dcc *DownloadClientsCreate) SetOrdering(i int) *DownloadClientsCreate { + dcc.mutation.SetOrdering(i) return dcc } -// SetNillablePriority sets the "priority" field if the given value is not nil. -func (dcc *DownloadClientsCreate) SetNillablePriority(s *string) *DownloadClientsCreate { - if s != nil { - dcc.SetPriority(*s) +// SetNillableOrdering sets the "ordering" field if the given value is not nil. +func (dcc *DownloadClientsCreate) SetNillableOrdering(i *int) *DownloadClientsCreate { + if i != nil { + dcc.SetOrdering(*i) } return dcc } @@ -188,9 +188,9 @@ func (dcc *DownloadClientsCreate) defaults() { v := downloadclients.DefaultSettings dcc.mutation.SetSettings(v) } - if _, ok := dcc.mutation.Priority(); !ok { - v := downloadclients.DefaultPriority - dcc.mutation.SetPriority(v) + if _, ok := dcc.mutation.Ordering(); !ok { + v := downloadclients.DefaultOrdering + dcc.mutation.SetOrdering(v) } if _, ok := dcc.mutation.RemoveCompletedDownloads(); !ok { v := downloadclients.DefaultRemoveCompletedDownloads @@ -217,6 +217,11 @@ func (dcc *DownloadClientsCreate) check() error { if _, ok := dcc.mutation.Implementation(); !ok { return &ValidationError{Name: "implementation", err: errors.New(`ent: missing required field "DownloadClients.implementation"`)} } + if v, ok := dcc.mutation.Implementation(); ok { + if err := downloadclients.ImplementationValidator(v); err != nil { + return &ValidationError{Name: "implementation", err: fmt.Errorf(`ent: validator failed for field "DownloadClients.implementation": %w`, err)} + } + } if _, ok := dcc.mutation.URL(); !ok { return &ValidationError{Name: "url", err: errors.New(`ent: missing required field "DownloadClients.url"`)} } @@ -229,8 +234,13 @@ func (dcc *DownloadClientsCreate) check() error { if _, ok := dcc.mutation.Settings(); !ok { return &ValidationError{Name: "settings", err: errors.New(`ent: missing required field "DownloadClients.settings"`)} } - if _, ok := dcc.mutation.Priority(); !ok { - return &ValidationError{Name: "priority", err: errors.New(`ent: missing required field "DownloadClients.priority"`)} + if _, ok := dcc.mutation.Ordering(); !ok { + return &ValidationError{Name: "ordering", err: errors.New(`ent: missing required field "DownloadClients.ordering"`)} + } + if v, ok := dcc.mutation.Ordering(); ok { + if err := downloadclients.OrderingValidator(v); err != nil { + return &ValidationError{Name: "ordering", err: fmt.Errorf(`ent: validator failed for field "DownloadClients.ordering": %w`, err)} + } } if _, ok := dcc.mutation.RemoveCompletedDownloads(); !ok { return &ValidationError{Name: "remove_completed_downloads", err: errors.New(`ent: missing required field "DownloadClients.remove_completed_downloads"`)} @@ -276,7 +286,7 @@ func (dcc *DownloadClientsCreate) createSpec() (*DownloadClients, *sqlgraph.Crea _node.Name = value } if value, ok := dcc.mutation.Implementation(); ok { - _spec.SetField(downloadclients.FieldImplementation, field.TypeString, value) + _spec.SetField(downloadclients.FieldImplementation, field.TypeEnum, value) _node.Implementation = value } if value, ok := dcc.mutation.URL(); ok { @@ -295,9 +305,9 @@ func (dcc *DownloadClientsCreate) createSpec() (*DownloadClients, *sqlgraph.Crea _spec.SetField(downloadclients.FieldSettings, field.TypeString, value) _node.Settings = value } - if value, ok := dcc.mutation.Priority(); ok { - _spec.SetField(downloadclients.FieldPriority, field.TypeString, value) - _node.Priority = value + if value, ok := dcc.mutation.Ordering(); ok { + _spec.SetField(downloadclients.FieldOrdering, field.TypeInt, value) + _node.Ordering = value } if value, ok := dcc.mutation.RemoveCompletedDownloads(); ok { _spec.SetField(downloadclients.FieldRemoveCompletedDownloads, field.TypeBool, value) diff --git a/ent/downloadclients_update.go b/ent/downloadclients_update.go index 113507f..ca7cc27 100644 --- a/ent/downloadclients_update.go +++ b/ent/downloadclients_update.go @@ -56,15 +56,15 @@ func (dcu *DownloadClientsUpdate) SetNillableName(s *string) *DownloadClientsUpd } // SetImplementation sets the "implementation" field. -func (dcu *DownloadClientsUpdate) SetImplementation(s string) *DownloadClientsUpdate { - dcu.mutation.SetImplementation(s) +func (dcu *DownloadClientsUpdate) SetImplementation(d downloadclients.Implementation) *DownloadClientsUpdate { + dcu.mutation.SetImplementation(d) return dcu } // SetNillableImplementation sets the "implementation" field if the given value is not nil. -func (dcu *DownloadClientsUpdate) SetNillableImplementation(s *string) *DownloadClientsUpdate { - if s != nil { - dcu.SetImplementation(*s) +func (dcu *DownloadClientsUpdate) SetNillableImplementation(d *downloadclients.Implementation) *DownloadClientsUpdate { + if d != nil { + dcu.SetImplementation(*d) } return dcu } @@ -125,20 +125,27 @@ func (dcu *DownloadClientsUpdate) SetNillableSettings(s *string) *DownloadClient return dcu } -// SetPriority sets the "priority" field. -func (dcu *DownloadClientsUpdate) SetPriority(s string) *DownloadClientsUpdate { - dcu.mutation.SetPriority(s) +// SetOrdering sets the "ordering" field. +func (dcu *DownloadClientsUpdate) SetOrdering(i int) *DownloadClientsUpdate { + dcu.mutation.ResetOrdering() + dcu.mutation.SetOrdering(i) return dcu } -// SetNillablePriority sets the "priority" field if the given value is not nil. -func (dcu *DownloadClientsUpdate) SetNillablePriority(s *string) *DownloadClientsUpdate { - if s != nil { - dcu.SetPriority(*s) +// SetNillableOrdering sets the "ordering" field if the given value is not nil. +func (dcu *DownloadClientsUpdate) SetNillableOrdering(i *int) *DownloadClientsUpdate { + if i != nil { + dcu.SetOrdering(*i) } return dcu } +// AddOrdering adds i to the "ordering" field. +func (dcu *DownloadClientsUpdate) AddOrdering(i int) *DownloadClientsUpdate { + dcu.mutation.AddOrdering(i) + return dcu +} + // SetRemoveCompletedDownloads sets the "remove_completed_downloads" field. func (dcu *DownloadClientsUpdate) SetRemoveCompletedDownloads(b bool) *DownloadClientsUpdate { dcu.mutation.SetRemoveCompletedDownloads(b) @@ -213,7 +220,25 @@ func (dcu *DownloadClientsUpdate) ExecX(ctx context.Context) { } } +// check runs all checks and user-defined validators on the builder. +func (dcu *DownloadClientsUpdate) check() error { + if v, ok := dcu.mutation.Implementation(); ok { + if err := downloadclients.ImplementationValidator(v); err != nil { + return &ValidationError{Name: "implementation", err: fmt.Errorf(`ent: validator failed for field "DownloadClients.implementation": %w`, err)} + } + } + if v, ok := dcu.mutation.Ordering(); ok { + if err := downloadclients.OrderingValidator(v); err != nil { + return &ValidationError{Name: "ordering", err: fmt.Errorf(`ent: validator failed for field "DownloadClients.ordering": %w`, err)} + } + } + return nil +} + func (dcu *DownloadClientsUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := dcu.check(); err != nil { + return n, err + } _spec := sqlgraph.NewUpdateSpec(downloadclients.Table, downloadclients.Columns, sqlgraph.NewFieldSpec(downloadclients.FieldID, field.TypeInt)) if ps := dcu.mutation.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { @@ -229,7 +254,7 @@ func (dcu *DownloadClientsUpdate) sqlSave(ctx context.Context) (n int, err error _spec.SetField(downloadclients.FieldName, field.TypeString, value) } if value, ok := dcu.mutation.Implementation(); ok { - _spec.SetField(downloadclients.FieldImplementation, field.TypeString, value) + _spec.SetField(downloadclients.FieldImplementation, field.TypeEnum, value) } if value, ok := dcu.mutation.URL(); ok { _spec.SetField(downloadclients.FieldURL, field.TypeString, value) @@ -243,8 +268,11 @@ func (dcu *DownloadClientsUpdate) sqlSave(ctx context.Context) (n int, err error if value, ok := dcu.mutation.Settings(); ok { _spec.SetField(downloadclients.FieldSettings, field.TypeString, value) } - if value, ok := dcu.mutation.Priority(); ok { - _spec.SetField(downloadclients.FieldPriority, field.TypeString, value) + if value, ok := dcu.mutation.Ordering(); ok { + _spec.SetField(downloadclients.FieldOrdering, field.TypeInt, value) + } + if value, ok := dcu.mutation.AddedOrdering(); ok { + _spec.AddField(downloadclients.FieldOrdering, field.TypeInt, value) } if value, ok := dcu.mutation.RemoveCompletedDownloads(); ok { _spec.SetField(downloadclients.FieldRemoveCompletedDownloads, field.TypeBool, value) @@ -304,15 +332,15 @@ func (dcuo *DownloadClientsUpdateOne) SetNillableName(s *string) *DownloadClient } // SetImplementation sets the "implementation" field. -func (dcuo *DownloadClientsUpdateOne) SetImplementation(s string) *DownloadClientsUpdateOne { - dcuo.mutation.SetImplementation(s) +func (dcuo *DownloadClientsUpdateOne) SetImplementation(d downloadclients.Implementation) *DownloadClientsUpdateOne { + dcuo.mutation.SetImplementation(d) return dcuo } // SetNillableImplementation sets the "implementation" field if the given value is not nil. -func (dcuo *DownloadClientsUpdateOne) SetNillableImplementation(s *string) *DownloadClientsUpdateOne { - if s != nil { - dcuo.SetImplementation(*s) +func (dcuo *DownloadClientsUpdateOne) SetNillableImplementation(d *downloadclients.Implementation) *DownloadClientsUpdateOne { + if d != nil { + dcuo.SetImplementation(*d) } return dcuo } @@ -373,20 +401,27 @@ func (dcuo *DownloadClientsUpdateOne) SetNillableSettings(s *string) *DownloadCl return dcuo } -// SetPriority sets the "priority" field. -func (dcuo *DownloadClientsUpdateOne) SetPriority(s string) *DownloadClientsUpdateOne { - dcuo.mutation.SetPriority(s) +// SetOrdering sets the "ordering" field. +func (dcuo *DownloadClientsUpdateOne) SetOrdering(i int) *DownloadClientsUpdateOne { + dcuo.mutation.ResetOrdering() + dcuo.mutation.SetOrdering(i) return dcuo } -// SetNillablePriority sets the "priority" field if the given value is not nil. -func (dcuo *DownloadClientsUpdateOne) SetNillablePriority(s *string) *DownloadClientsUpdateOne { - if s != nil { - dcuo.SetPriority(*s) +// SetNillableOrdering sets the "ordering" field if the given value is not nil. +func (dcuo *DownloadClientsUpdateOne) SetNillableOrdering(i *int) *DownloadClientsUpdateOne { + if i != nil { + dcuo.SetOrdering(*i) } return dcuo } +// AddOrdering adds i to the "ordering" field. +func (dcuo *DownloadClientsUpdateOne) AddOrdering(i int) *DownloadClientsUpdateOne { + dcuo.mutation.AddOrdering(i) + return dcuo +} + // SetRemoveCompletedDownloads sets the "remove_completed_downloads" field. func (dcuo *DownloadClientsUpdateOne) SetRemoveCompletedDownloads(b bool) *DownloadClientsUpdateOne { dcuo.mutation.SetRemoveCompletedDownloads(b) @@ -474,7 +509,25 @@ func (dcuo *DownloadClientsUpdateOne) ExecX(ctx context.Context) { } } +// check runs all checks and user-defined validators on the builder. +func (dcuo *DownloadClientsUpdateOne) check() error { + if v, ok := dcuo.mutation.Implementation(); ok { + if err := downloadclients.ImplementationValidator(v); err != nil { + return &ValidationError{Name: "implementation", err: fmt.Errorf(`ent: validator failed for field "DownloadClients.implementation": %w`, err)} + } + } + if v, ok := dcuo.mutation.Ordering(); ok { + if err := downloadclients.OrderingValidator(v); err != nil { + return &ValidationError{Name: "ordering", err: fmt.Errorf(`ent: validator failed for field "DownloadClients.ordering": %w`, err)} + } + } + return nil +} + func (dcuo *DownloadClientsUpdateOne) sqlSave(ctx context.Context) (_node *DownloadClients, err error) { + if err := dcuo.check(); err != nil { + return _node, err + } _spec := sqlgraph.NewUpdateSpec(downloadclients.Table, downloadclients.Columns, sqlgraph.NewFieldSpec(downloadclients.FieldID, field.TypeInt)) id, ok := dcuo.mutation.ID() if !ok { @@ -507,7 +560,7 @@ func (dcuo *DownloadClientsUpdateOne) sqlSave(ctx context.Context) (_node *Downl _spec.SetField(downloadclients.FieldName, field.TypeString, value) } if value, ok := dcuo.mutation.Implementation(); ok { - _spec.SetField(downloadclients.FieldImplementation, field.TypeString, value) + _spec.SetField(downloadclients.FieldImplementation, field.TypeEnum, value) } if value, ok := dcuo.mutation.URL(); ok { _spec.SetField(downloadclients.FieldURL, field.TypeString, value) @@ -521,8 +574,11 @@ func (dcuo *DownloadClientsUpdateOne) sqlSave(ctx context.Context) (_node *Downl if value, ok := dcuo.mutation.Settings(); ok { _spec.SetField(downloadclients.FieldSettings, field.TypeString, value) } - if value, ok := dcuo.mutation.Priority(); ok { - _spec.SetField(downloadclients.FieldPriority, field.TypeString, value) + if value, ok := dcuo.mutation.Ordering(); ok { + _spec.SetField(downloadclients.FieldOrdering, field.TypeInt, value) + } + if value, ok := dcuo.mutation.AddedOrdering(); ok { + _spec.AddField(downloadclients.FieldOrdering, field.TypeInt, value) } if value, ok := dcuo.mutation.RemoveCompletedDownloads(); ok { _spec.SetField(downloadclients.FieldRemoveCompletedDownloads, field.TypeBool, value) diff --git a/ent/migrate/schema.go b/ent/migrate/schema.go index e05b17f..0d0b7c4 100644 --- a/ent/migrate/schema.go +++ b/ent/migrate/schema.go @@ -25,12 +25,12 @@ var ( {Name: "id", Type: field.TypeInt, Increment: true}, {Name: "enable", Type: field.TypeBool}, {Name: "name", Type: field.TypeString}, - {Name: "implementation", Type: field.TypeString}, + {Name: "implementation", Type: field.TypeEnum, Enums: []string{"transmission", "qbittorrent"}}, {Name: "url", Type: field.TypeString}, {Name: "user", Type: field.TypeString, Default: ""}, {Name: "password", Type: field.TypeString, Default: ""}, {Name: "settings", Type: field.TypeString, Default: ""}, - {Name: "priority", Type: field.TypeString, Default: ""}, + {Name: "ordering", Type: field.TypeInt, Default: 1}, {Name: "remove_completed_downloads", Type: field.TypeBool, Default: true}, {Name: "remove_failed_downloads", Type: field.TypeBool, Default: true}, {Name: "tags", Type: field.TypeString, Default: ""}, diff --git a/ent/mutation.go b/ent/mutation.go index 173dc22..618398a 100644 --- a/ent/mutation.go +++ b/ent/mutation.go @@ -434,12 +434,13 @@ type DownloadClientsMutation struct { id *int enable *bool name *string - implementation *string + implementation *downloadclients.Implementation url *string user *string password *string settings *string - priority *string + ordering *int + addordering *int remove_completed_downloads *bool remove_failed_downloads *bool tags *string @@ -620,12 +621,12 @@ func (m *DownloadClientsMutation) ResetName() { } // SetImplementation sets the "implementation" field. -func (m *DownloadClientsMutation) SetImplementation(s string) { - m.implementation = &s +func (m *DownloadClientsMutation) SetImplementation(d downloadclients.Implementation) { + m.implementation = &d } // Implementation returns the value of the "implementation" field in the mutation. -func (m *DownloadClientsMutation) Implementation() (r string, exists bool) { +func (m *DownloadClientsMutation) Implementation() (r downloadclients.Implementation, exists bool) { v := m.implementation if v == nil { return @@ -636,7 +637,7 @@ func (m *DownloadClientsMutation) Implementation() (r string, exists bool) { // OldImplementation returns the old "implementation" field's value of the DownloadClients entity. // If the DownloadClients object wasn't provided to the builder, the object is fetched from the database. // An error is returned if the mutation operation is not UpdateOne, or the database query fails. -func (m *DownloadClientsMutation) OldImplementation(ctx context.Context) (v string, err error) { +func (m *DownloadClientsMutation) OldImplementation(ctx context.Context) (v downloadclients.Implementation, err error) { if !m.op.Is(OpUpdateOne) { return v, errors.New("OldImplementation is only allowed on UpdateOne operations") } @@ -799,40 +800,60 @@ func (m *DownloadClientsMutation) ResetSettings() { m.settings = nil } -// SetPriority sets the "priority" field. -func (m *DownloadClientsMutation) SetPriority(s string) { - m.priority = &s +// SetOrdering sets the "ordering" field. +func (m *DownloadClientsMutation) SetOrdering(i int) { + m.ordering = &i + m.addordering = nil } -// Priority returns the value of the "priority" field in the mutation. -func (m *DownloadClientsMutation) Priority() (r string, exists bool) { - v := m.priority +// Ordering returns the value of the "ordering" field in the mutation. +func (m *DownloadClientsMutation) Ordering() (r int, exists bool) { + v := m.ordering if v == nil { return } return *v, true } -// OldPriority returns the old "priority" field's value of the DownloadClients entity. +// OldOrdering returns the old "ordering" field's value of the DownloadClients entity. // If the DownloadClients object wasn't provided to the builder, the object is fetched from the database. // An error is returned if the mutation operation is not UpdateOne, or the database query fails. -func (m *DownloadClientsMutation) OldPriority(ctx context.Context) (v string, err error) { +func (m *DownloadClientsMutation) OldOrdering(ctx context.Context) (v int, err error) { if !m.op.Is(OpUpdateOne) { - return v, errors.New("OldPriority is only allowed on UpdateOne operations") + return v, errors.New("OldOrdering is only allowed on UpdateOne operations") } if m.id == nil || m.oldValue == nil { - return v, errors.New("OldPriority requires an ID field in the mutation") + return v, errors.New("OldOrdering requires an ID field in the mutation") } oldValue, err := m.oldValue(ctx) if err != nil { - return v, fmt.Errorf("querying old value for OldPriority: %w", err) + return v, fmt.Errorf("querying old value for OldOrdering: %w", err) } - return oldValue.Priority, nil + return oldValue.Ordering, nil } -// ResetPriority resets all changes to the "priority" field. -func (m *DownloadClientsMutation) ResetPriority() { - m.priority = nil +// AddOrdering adds i to the "ordering" field. +func (m *DownloadClientsMutation) AddOrdering(i int) { + if m.addordering != nil { + *m.addordering += i + } else { + m.addordering = &i + } +} + +// AddedOrdering returns the value that was added to the "ordering" field in this mutation. +func (m *DownloadClientsMutation) AddedOrdering() (r int, exists bool) { + v := m.addordering + if v == nil { + return + } + return *v, true +} + +// ResetOrdering resets all changes to the "ordering" field. +func (m *DownloadClientsMutation) ResetOrdering() { + m.ordering = nil + m.addordering = nil } // SetRemoveCompletedDownloads sets the "remove_completed_downloads" field. @@ -999,8 +1020,8 @@ func (m *DownloadClientsMutation) Fields() []string { if m.settings != nil { fields = append(fields, downloadclients.FieldSettings) } - if m.priority != nil { - fields = append(fields, downloadclients.FieldPriority) + if m.ordering != nil { + fields = append(fields, downloadclients.FieldOrdering) } if m.remove_completed_downloads != nil { fields = append(fields, downloadclients.FieldRemoveCompletedDownloads) @@ -1033,8 +1054,8 @@ func (m *DownloadClientsMutation) Field(name string) (ent.Value, bool) { return m.Password() case downloadclients.FieldSettings: return m.Settings() - case downloadclients.FieldPriority: - return m.Priority() + case downloadclients.FieldOrdering: + return m.Ordering() case downloadclients.FieldRemoveCompletedDownloads: return m.RemoveCompletedDownloads() case downloadclients.FieldRemoveFailedDownloads: @@ -1064,8 +1085,8 @@ func (m *DownloadClientsMutation) OldField(ctx context.Context, name string) (en return m.OldPassword(ctx) case downloadclients.FieldSettings: return m.OldSettings(ctx) - case downloadclients.FieldPriority: - return m.OldPriority(ctx) + case downloadclients.FieldOrdering: + return m.OldOrdering(ctx) case downloadclients.FieldRemoveCompletedDownloads: return m.OldRemoveCompletedDownloads(ctx) case downloadclients.FieldRemoveFailedDownloads: @@ -1096,7 +1117,7 @@ func (m *DownloadClientsMutation) SetField(name string, value ent.Value) error { m.SetName(v) return nil case downloadclients.FieldImplementation: - v, ok := value.(string) + v, ok := value.(downloadclients.Implementation) if !ok { return fmt.Errorf("unexpected type %T for field %s", value, name) } @@ -1130,12 +1151,12 @@ func (m *DownloadClientsMutation) SetField(name string, value ent.Value) error { } m.SetSettings(v) return nil - case downloadclients.FieldPriority: - v, ok := value.(string) + case downloadclients.FieldOrdering: + v, ok := value.(int) if !ok { return fmt.Errorf("unexpected type %T for field %s", value, name) } - m.SetPriority(v) + m.SetOrdering(v) return nil case downloadclients.FieldRemoveCompletedDownloads: v, ok := value.(bool) @@ -1165,13 +1186,21 @@ func (m *DownloadClientsMutation) SetField(name string, value ent.Value) error { // AddedFields returns all numeric fields that were incremented/decremented during // this mutation. func (m *DownloadClientsMutation) AddedFields() []string { - return nil + var fields []string + if m.addordering != nil { + fields = append(fields, downloadclients.FieldOrdering) + } + return fields } // AddedField returns the numeric value that was incremented/decremented on a field // with the given name. The second boolean return value indicates that this field // was not set, or was not defined in the schema. func (m *DownloadClientsMutation) AddedField(name string) (ent.Value, bool) { + switch name { + case downloadclients.FieldOrdering: + return m.AddedOrdering() + } return nil, false } @@ -1180,6 +1209,13 @@ func (m *DownloadClientsMutation) AddedField(name string) (ent.Value, bool) { // type. func (m *DownloadClientsMutation) AddField(name string, value ent.Value) error { switch name { + case downloadclients.FieldOrdering: + v, ok := value.(int) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.AddOrdering(v) + return nil } return fmt.Errorf("unknown DownloadClients numeric field %s", name) } @@ -1228,8 +1264,8 @@ func (m *DownloadClientsMutation) ResetField(name string) error { case downloadclients.FieldSettings: m.ResetSettings() return nil - case downloadclients.FieldPriority: - m.ResetPriority() + case downloadclients.FieldOrdering: + m.ResetOrdering() return nil case downloadclients.FieldRemoveCompletedDownloads: m.ResetRemoveCompletedDownloads() diff --git a/ent/runtime.go b/ent/runtime.go index 8418bbe..dc98eba 100644 --- a/ent/runtime.go +++ b/ent/runtime.go @@ -32,10 +32,12 @@ func init() { downloadclientsDescSettings := downloadclientsFields[6].Descriptor() // downloadclients.DefaultSettings holds the default value on creation for the settings field. downloadclients.DefaultSettings = downloadclientsDescSettings.Default.(string) - // downloadclientsDescPriority is the schema descriptor for priority field. - downloadclientsDescPriority := downloadclientsFields[7].Descriptor() - // downloadclients.DefaultPriority holds the default value on creation for the priority field. - downloadclients.DefaultPriority = downloadclientsDescPriority.Default.(string) + // downloadclientsDescOrdering is the schema descriptor for ordering field. + downloadclientsDescOrdering := downloadclientsFields[7].Descriptor() + // downloadclients.DefaultOrdering holds the default value on creation for the ordering field. + downloadclients.DefaultOrdering = downloadclientsDescOrdering.Default.(int) + // downloadclients.OrderingValidator is a validator for the "ordering" field. It is called by the builders before save. + downloadclients.OrderingValidator = downloadclientsDescOrdering.Validators[0].(func(int) error) // downloadclientsDescRemoveCompletedDownloads is the schema descriptor for remove_completed_downloads field. downloadclientsDescRemoveCompletedDownloads := downloadclientsFields[8].Descriptor() // downloadclients.DefaultRemoveCompletedDownloads holds the default value on creation for the remove_completed_downloads field. diff --git a/ent/schema/downloadclients.go b/ent/schema/downloadclients.go index 3ee1fa7..23e419f 100644 --- a/ent/schema/downloadclients.go +++ b/ent/schema/downloadclients.go @@ -1,6 +1,8 @@ package schema import ( + "errors" + "entgo.io/ent" "entgo.io/ent/schema/field" ) @@ -15,12 +17,20 @@ func (DownloadClients) Fields() []ent.Field { return []ent.Field{ field.Bool("enable"), field.String("name"), - field.String("implementation"), + field.Enum("implementation").Values("transmission", "qbittorrent"), field.String("url"), field.String("user").Default(""), field.String("password").Default(""), field.String("settings").Default(""), - field.String("priority").Default(""), + field.Int("ordering").Default(1).Validate(func(i int) error { + if i > 50 { + return errors.ErrUnsupported + } + if i <= 0 { + return errors.ErrUnsupported + } + return nil + }), field.Bool("remove_completed_downloads").Default(true), field.Bool("remove_failed_downloads").Default(true), field.String("tags").Default(""), diff --git a/go.sum b/go.sum index 7bd1c32..0cbb489 100644 --- a/go.sum +++ b/go.sum @@ -106,6 +106,8 @@ github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0V github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM= @@ -125,6 +127,8 @@ github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g= github.com/nikoksr/notify v1.0.0 h1:qe9/6FRsWdxBgQgWcpvQ0sv8LRGJZDpRB4TkL2uNdO8= github.com/nikoksr/notify v1.0.0/go.mod h1:hPaaDt30d6LAA7/5nb0e48Bp/MctDfycCSs8VEgN29I= +github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= +github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -148,6 +152,8 @@ github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= @@ -240,6 +246,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg= +golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= diff --git a/pkg/doc.go b/pkg/doc.go index 9409421..984e642 100644 --- a/pkg/doc.go +++ b/pkg/doc.go @@ -9,9 +9,13 @@ type Torrent interface { Save() string Exists() bool SeedRatio() (float64, error) + GetHash() string } +type Downloader interface { + GetAll() ([]Torrent, error) + Download(link, dir string) (Torrent, error) +} type Storage interface { - -} \ No newline at end of file +} diff --git a/pkg/qbittorrent/qbittorrent.go b/pkg/qbittorrent/qbittorrent.go index 5a11e8e..eaee609 100644 --- a/pkg/qbittorrent/qbittorrent.go +++ b/pkg/qbittorrent/qbittorrent.go @@ -10,8 +10,49 @@ import ( "github.com/pkg/errors" ) +type Info struct { + URL string + User string + Password string +} + type Client struct { c *qbt.Client + Info +} + +func NewClient(url, user, pass string) (*Client, error) { + // connect to qbittorrent client + qb := qbt.NewClient(url) + + // login to the client + loginOpts := qbt.LoginOptions{ + Username: user, + Password: pass, + } + err := qb.Login(loginOpts) + if err != nil { + return nil, err + } + + return &Client{c: qb, Info: Info{URL: url, User: user, Password: pass}}, nil +} + +func (c *Client) GetAll() ([]pkg.Torrent, error) { + tt, err :=c.c.Torrents(qbt.TorrentsOptions{}) + if err != nil { + return nil, errors.Wrap(err, "get torrents") + } + var res []pkg.Torrent + for _, t := range tt { + t1 := &Torrent{ + c: c.c, + Hash: t.Hash, + Info: c.Info, + } + res = append(res, t1) + } + return res, nil } func (c *Client) Download(link, dir string) (pkg.Torrent, error) { @@ -48,16 +89,18 @@ loop: if newHash == "" { return nil, fmt.Errorf("download torrent fail: timeout") } - return &Torrent{Hash: newHash, c: c.c}, nil + return &Torrent{Hash: newHash, c: c.c, Info: c.Info}, nil } type Torrent struct { - c *qbt.Client - Hash string - URL string - User string - Password string + c *qbt.Client + Hash string + Info +} + +func (t *Torrent) GetHash() string { + return t.Hash } func (t *Torrent) getTorrent() (*qbt.TorrentInfo, error) { diff --git a/pkg/transmission/transmission.go b/pkg/transmission/transmission.go index 71e3b10..88b27a2 100644 --- a/pkg/transmission/transmission.go +++ b/pkg/transmission/transmission.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "polaris/log" + "polaris/pkg" "strings" "github.com/hekmon/transmissionrpc/v3" @@ -45,12 +46,12 @@ type Client struct { cfg Config } -func (c *Client) GetAll() ([]*Torrent, error) { +func (c *Client) GetAll() ([]pkg.Torrent, error) { all, err := c.c.TorrentGetAll(context.TODO()) if err != nil { return nil, errors.Wrap(err, "get all") } - var torrents []*Torrent + var torrents []pkg.Torrent for _, t := range all { torrents = append(torrents, &Torrent{ Hash: *t.HashString, @@ -61,7 +62,7 @@ func (c *Client) GetAll() ([]*Torrent, error) { return torrents, nil } -func (c *Client) Download(link, dir string) (*Torrent, error) { +func (c *Client) Download(link, dir string) (pkg.Torrent, error) { if strings.HasPrefix(link, "http") { client := &http.Client{ CheckRedirect: func(req *http.Request, via []*http.Request) error { @@ -207,6 +208,11 @@ func (t *Torrent) Save() string { return string(d) } +func (t *Torrent) GetHash() string { + return t.Hash +} + + func ReloadTorrent(s string) (*Torrent, error) { var torrent = Torrent{} err := json.Unmarshal([]byte(s), &torrent) diff --git a/server/activity.go b/server/activity.go index 171c697..9f4c3bb 100644 --- a/server/activity.go +++ b/server/activity.go @@ -123,7 +123,7 @@ type TorrentInfo struct { } func (s *Server) GetAllTorrents(c *gin.Context) (interface{}, error) { - trc, _, err := s.getDownloadClient() + trc, _, err := s.core.GetDownloadClient() if err != nil { return nil, errors.Wrap(err, "connect transmission") } @@ -140,7 +140,7 @@ func (s *Server) GetAllTorrents(c *gin.Context) (interface{}, error) { p, _ := t.Progress() infos = append(infos, TorrentInfo{ Name: name, - ID: t.Hash, + ID: t.GetHash(), Progress: p, }) } diff --git a/server/core/client.go b/server/core/client.go index 7271c40..98eefe6 100644 --- a/server/core/client.go +++ b/server/core/client.go @@ -3,7 +3,10 @@ package core import ( "polaris/db" "polaris/ent" + "polaris/ent/downloadclients" "polaris/log" + "polaris/pkg" + "polaris/pkg/qbittorrent" "polaris/pkg/tmdb" "polaris/pkg/transmission" "polaris/pkg/utils" @@ -61,17 +64,34 @@ func (c *Client) reloadTasks() { } } -func (c *Client) getDownloadClient() (*transmission.Client, *ent.DownloadClients, error) { - tr := c.db.GetTransmission() - trc, err := transmission.NewClient(transmission.Config{ - URL: tr.URL, - User: tr.User, - Password: tr.Password, - }) - if err != nil { - return nil, nil, errors.Wrap(err, "connect transmission") +func (c *Client) GetDownloadClient() (pkg.Downloader, *ent.DownloadClients, error) { + downloaders := c.db.GetAllDonloadClients() + for _, d := range downloaders { + if !d.Enable { + continue + } + if d.Implementation == downloadclients.ImplementationTransmission { + trc, err := transmission.NewClient(transmission.Config{ + URL: d.URL, + User: d.User, + Password: d.Password, + }) + if err != nil { + log.Warnf("connect to download client error: %v", d.URL) + continue + } + return trc, d, nil + + } else if d.Implementation == downloadclients.ImplementationQbittorrent { + qbt, err := qbittorrent.NewClient(d.URL, d.User, d.Password) + if err != nil { + log.Warnf("connect to download client error: %v", d.URL) + continue + } + return qbt, d, nil + } } - return trc, tr, nil + return nil, nil, errors.Errorf("no available download client") } func (c *Client) TMDB() (*tmdb.Client, error) { diff --git a/server/core/resources.go b/server/core/resources.go index cc5d740..9191baf 100644 --- a/server/core/resources.go +++ b/server/core/resources.go @@ -14,7 +14,7 @@ import ( ) func (c *Client) DownloadEpisodeTorrent(r1 torznab.Result, seriesId, seasonNum, episodeNum int) (*string, error) { - trc, dlc, err := c.getDownloadClient() + trc, dlc, err := c.GetDownloadClient() if err != nil { return nil, errors.Wrap(err, "connect transmission") } @@ -102,7 +102,7 @@ func (c *Client) SearchAndDownload(seriesId, seasonNum, episodeNum int) (*string } func (c *Client) DownloadMovie(m *ent.Media, link, name string, size int, indexerID int) (*string, error) { - trc, dlc, err := c.getDownloadClient() + trc, dlc, err := c.GetDownloadClient() if err != nil { return nil, errors.Wrap(err, "connect transmission") } diff --git a/server/core/scheduler.go b/server/core/scheduler.go index efcd98d..ebebf96 100644 --- a/server/core/scheduler.go +++ b/server/core/scheduler.go @@ -341,7 +341,7 @@ func (c *Client) DownloadMovieByID(id int) (string, error) { } func (c *Client) downloadMovieSingleEpisode(ep *ent.Episode, targetDir string) (string, error) { - trc, dlc, err := c.getDownloadClient() + trc, dlc, err := c.GetDownloadClient() if err != nil { return "", errors.Wrap(err, "connect transmission") } diff --git a/server/setting.go b/server/setting.go index 26861c2..b0384b7 100644 --- a/server/setting.go +++ b/server/setting.go @@ -6,7 +6,9 @@ import ( "html/template" "polaris/db" "polaris/ent" + "polaris/ent/downloadclients" "polaris/log" + "polaris/pkg/qbittorrent" "polaris/pkg/transmission" "polaris/pkg/utils" "strconv" @@ -189,20 +191,6 @@ func (s *Server) GetAllIndexers(c *gin.Context) (interface{}, error) { } return indexers, nil } - -func (s *Server) getDownloadClient() (*transmission.Client, *ent.DownloadClients, error) { - tr := s.db.GetTransmission() - trc, err := transmission.NewClient(transmission.Config{ - URL: tr.URL, - User: tr.User, - Password: tr.Password, - }) - if err != nil { - return nil, nil, errors.Wrap(err, "connect transmission") - } - return trc, tr, nil -} - type downloadClientIn struct { Name string `json:"name" binding:"required"` URL string `json:"url" binding:"required"` @@ -218,16 +206,30 @@ func (s *Server) AddDownloadClient(c *gin.Context) (interface{}, error) { } utils.TrimFields(&in) //test connection - _, err := transmission.NewClient(transmission.Config{ - URL: in.URL, - User: in.User, - Password: in.Password, - }) - if err != nil { - return nil, errors.Wrap(err, "tranmission setting") + if in.Implementation == downloadclients.ImplementationTransmission.String() { + _, err := transmission.NewClient(transmission.Config{ + URL: in.URL, + User: in.User, + Password: in.Password, + }) + if err != nil { + return nil, errors.Wrap(err, "tranmission setting") + } + + } else if in.Implementation == downloadclients.ImplementationQbittorrent.String() { + _, err := qbittorrent.NewClient(in.URL, in.User, in.Password) + if err != nil { + return nil, errors.Wrap(err, "qbittorrent") + } } - if err := s.db.SaveTransmission(in.Name, in.URL, in.User, in.Password); err != nil { - return nil, errors.Wrap(err, "save transmission") + if err := s.db.SaveDownloader(&ent.DownloadClients{ + Name: in.Name, + Implementation: downloadclients.Implementation(in.Implementation), + URL: in.URL, + User: in.User, + Password: in.Password, + }); err != nil { + return nil, errors.Wrap(err, "save downloader") } return nil, nil } diff --git a/ui/lib/settings/downloader.dart b/ui/lib/settings/downloader.dart index cfd8fc4..8711f8d 100644 --- a/ui/lib/settings/downloader.dart +++ b/ui/lib/settings/downloader.dart @@ -53,7 +53,7 @@ class _DownloaderState extends ConsumerState { "url": client.url, "user": client.user, "password": client.password, - "impl": "transmission", + "impl": client.implementation, "remove_completed_downloads": client.removeCompletedDownloads, "remove_failed_downloads": client.removeFailedDownloads, }, @@ -70,6 +70,8 @@ class _DownloaderState extends ConsumerState { items: const [ DropdownMenuItem( value: "transmission", child: Text("Transmission")), + DropdownMenuItem( + value: "qbittorrent", child: Text("qBittorrent")), ], ), FormBuilderTextField(