Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: scope errors #204

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions internal/provider/data_source_remote_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ func dataSourceRemoteFileRead(ctx context.Context, d *schema.ResourceData, meta
return diag.Errorf(err.Error())
}

err = setResourceID(d, conn)
if err != nil {
if err := setResourceID(d, conn); err != nil {
return diag.Errorf(err.Error())
}

Expand Down Expand Up @@ -131,8 +130,7 @@ func dataSourceRemoteFileRead(ctx context.Context, d *schema.ResourceData, meta
}
d.Set("group_name", group_name)

err = meta.(*apiClient).closeRemoteClient(conn)
if err != nil {
if err := meta.(*apiClient).closeRemoteClient(conn); err != nil {
return diag.Errorf("unable to close remote client: %s", err.Error())
}

Expand Down
9 changes: 3 additions & 6 deletions internal/provider/remote_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ func (e Error) Error() string {
func run(s *ssh.Session, cmd string) error {
var b bytes.Buffer
s.Stderr = &b
err := s.Run(cmd)

if err != nil {
if err := s.Run(cmd); err != nil {
return Error{
cmd: cmd,
err: err,
Expand Down Expand Up @@ -143,9 +142,8 @@ func (c *RemoteClient) FileExists(path string, sudo bool) (bool, error) {
if sudo {
cmd = fmt.Sprintf("sudo %s", cmd)
}
err = run(session, cmd)

if err != nil {
if err := run(session, cmd); err != nil {
session2, err := sshClient.NewSession()
if err != nil {
return false, err
Expand Down Expand Up @@ -183,8 +181,7 @@ func (c *RemoteClient) ReadFileSFTP(path string) (string, error) {
defer file.Close()

content := bytes.Buffer{}
_, err = file.WriteTo(&content)
if err != nil {
if _, err := file.WriteTo(&content); err != nil {
return "", err
}

Expand Down
24 changes: 8 additions & 16 deletions internal/provider/resource_remote_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ func resourceRemoteFileCreate(ctx context.Context, d *schema.ResourceData, meta
return diag.Diagnostics{{Severity: diag.Error, Summary: err.Error()}}
}

err = setResourceID(d, conn)
if err != nil {
if err := setResourceID(d, conn); err != nil {
return diag.Errorf(err.Error())
}

Expand Down Expand Up @@ -139,21 +138,18 @@ func resourceRemoteFileCreate(ctx context.Context, d *schema.ResourceData, meta
}

if group != "" {
err = client.ChgrpFile(path, group, sudo)
if err != nil {
if err := client.ChgrpFile(path, group, sudo); err != nil {
return diag.Errorf("unable to change group of remote file: %s", err.Error())
}
}

if owner != "" {
err = client.ChownFile(path, owner, sudo)
if err != nil {
if err := client.ChownFile(path, owner, sudo); err != nil {
return diag.Errorf("unable to change owner of remote file: %s", err.Error())
}
}

err = meta.(*apiClient).closeRemoteClient(conn)
if err != nil {
if err := meta.(*apiClient).closeRemoteClient(conn); err != nil {
return diag.Errorf("unable to close remote client: %s", err.Error())
}

Expand All @@ -166,8 +162,7 @@ func resourceRemoteFileRead(ctx context.Context, d *schema.ResourceData, meta in
return diag.Errorf(err.Error())
}

err = setResourceID(d, conn)
if err != nil {
if err := setResourceID(d, conn); err != nil {
return diag.Errorf(err.Error())
}

Expand Down Expand Up @@ -256,8 +251,7 @@ func resourceRemoteFileRead(ctx context.Context, d *schema.ResourceData, meta in
d.SetId("")
}

err = meta.(*apiClient).closeRemoteClient(conn)
if err != nil {
if err := meta.(*apiClient).closeRemoteClient(conn); err != nil {
return diag.Errorf("unable to close remote client: %s", err.Error())
}

Expand Down Expand Up @@ -294,14 +288,12 @@ func resourceRemoteFileDelete(ctx context.Context, d *schema.ResourceData, meta
return diag.Errorf("unable to check if remote file exists: %s", err.Error())
}
if exists {
err = client.DeleteFile(path, sudo)
if err != nil {
if err := client.DeleteFile(path, sudo); err != nil {
return diag.Errorf("unable to delete remote file: %s", err.Error())
}
}

err = meta.(*apiClient).closeRemoteClient(conn)
if err != nil {
if err := meta.(*apiClient).closeRemoteClient(conn); err != nil {
return diag.Errorf("unable to close remote client: %s", err.Error())
}

Expand Down
Loading