-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
140 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cwhub | ||
|
||
import ( | ||
"github.com/crowdsecurity/crowdsec/pkg/emoji" | ||
) | ||
|
||
// ItemState is used to keep the local state (i.e. at runtime) of an item. | ||
// This data is not stored in the index, but is displayed with "cscli ... inspect". | ||
type ItemState struct { | ||
LocalPath string `json:"local_path,omitempty" yaml:"local_path,omitempty"` | ||
LocalVersion string `json:"local_version,omitempty" yaml:"local_version,omitempty"` | ||
LocalHash string `json:"local_hash,omitempty" yaml:"local_hash,omitempty"` | ||
Installed bool `json:"installed"` | ||
Downloaded bool `json:"downloaded"` | ||
UpToDate bool `json:"up_to_date"` | ||
Tainted bool `json:"tainted"` | ||
TaintedBy []string `json:"tainted_by,omitempty" yaml:"tainted_by,omitempty"` | ||
BelongsToCollections []string `json:"belongs_to_collections,omitempty" yaml:"belongs_to_collections,omitempty"` | ||
} | ||
|
||
// IsLocal returns true if the item has been create by a user (not downloaded from the hub). | ||
func (s *ItemState) IsLocal() bool { | ||
return s.Installed && !s.Downloaded | ||
} | ||
|
||
// Text returns the status of the item as a string (eg. "enabled,update-available"). | ||
func (s *ItemState) Text() string { | ||
ret := "disabled" | ||
|
||
if s.Installed { | ||
ret = "enabled" | ||
} | ||
|
||
if s.IsLocal() { | ||
ret += ",local" | ||
} | ||
|
||
if s.Tainted { | ||
ret += ",tainted" | ||
} else if !s.UpToDate && !s.IsLocal() { | ||
ret += ",update-available" | ||
} | ||
|
||
return ret | ||
} | ||
|
||
// Emoji returns the status of the item as an emoji (eg. emoji.Warning). | ||
func (s *ItemState) Emoji() string { | ||
switch { | ||
case s.IsLocal(): | ||
return emoji.House | ||
case !s.Installed: | ||
return emoji.Prohibited | ||
case s.Tainted || (!s.UpToDate && !s.IsLocal()): | ||
return emoji.Warning | ||
case s.Installed: | ||
return emoji.CheckMark | ||
default: | ||
return emoji.QuestionMark | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package cwhub | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestItemStateText(t *testing.T) { | ||
// Test the text representation of an item state. | ||
type test struct { | ||
state ItemState | ||
want string | ||
wantIcon string | ||
} | ||
|
||
tests := []test{ | ||
{ | ||
ItemState{ | ||
Installed: true, | ||
UpToDate: false, | ||
Tainted: false, | ||
Downloaded: true, | ||
}, | ||
"enabled,update-available", | ||
"", | ||
}, { | ||
ItemState{ | ||
Installed: true, | ||
UpToDate: true, | ||
Tainted: false, | ||
Downloaded: true, | ||
}, | ||
"enabled", | ||
"", | ||
}, { | ||
ItemState{ | ||
Installed: true, | ||
UpToDate: false, | ||
Tainted: false, | ||
Downloaded: false, | ||
}, | ||
"enabled,local", | ||
"", | ||
}, { | ||
ItemState{ | ||
Installed: false, | ||
UpToDate: false, | ||
Tainted: false, | ||
Downloaded: true, | ||
}, | ||
"disabled,update-available", | ||
"", | ||
}, { | ||
ItemState{ | ||
Installed: true, | ||
UpToDate: false, | ||
Tainted: true, | ||
Downloaded: true, | ||
}, | ||
"enabled,tainted", | ||
"", | ||
}, | ||
} | ||
|
||
for idx, tc := range tests { | ||
t.Run("Test "+strconv.Itoa(idx), func(t *testing.T) { | ||
got := tc.state.Text() | ||
assert.Equal(t, tc.want, got) | ||
assert.Equal(t, tc.wantIcon, tc.state.Emoji()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters